This article original address, my blog: jsbintask.cn/2019/04/15/… (edible effect is best), reproduced please indicate the source!

preface

The observer pattern defines a one-to-many dependency between objects, allowing some objects to dynamically perceive changes in state. When an object state changes, observers can make corresponding updates, making the system easier to expand, it is the idea of interface programming and flexible composition!

Code address: github.com/jsbintask22…

chestnuts

  1. Xiaoli is very beautiful, “born beautiful and hard to give up “, is a” goddess “.
  2. Xiao Li has a lot of “back-up boys” around her. They add xiao Li’s wechat in various ways, including “Xiao Hao and Xiao Wu”.
  3. Xiao Li always posts about her life on her moments.
  4. The “spare boys” always interact with the goddess in a timely and positive way!
  5. Xiao Li found that “spare tire” Xiao Hao did not like to interact, so she deleted “spare tire” Xiao Hao’s wechat.
  6. Howe found himself unable to see the goddess. Finally give up!
  7. Xiao Li met a new “spare tire” Xiao Li, so Xiao Li also added the goddess wechat.
  8. Xiaoli released his circle of friends dynamic, Xiaoli also began to interact! We can abstract two themes from this process,Small beautiful goddess.Spare Howe, Spare Wu, spare LiLet’s simulate this in codeAfter the goddessIn the process.

Code implementation

V1.0

Beauty stands for goddess, and LittleBoy stands for backup. They are always paying attention to the goddess’s circle of friends, hoping to get interaction. The code is as follows:

public class App {
    public static void main(String[] args) throws Exception{
        Beauty beauty = new Beauty();
        // Successfully added goddess wechat
        LittleBoy littleBoy = new LittleBoy(beauty);

        // Start checking goddess moments
        littleBoy.start();

        // After 5s, goddess released a circle of friends.
        Thread.sleep(5000L); beauty.publishWechat(); System.in.read(); }}Copy the code

The running results are as follows:

LittleBoy

V2.0

In order not to let LittleBoy always round to check the goddess status, we can change the goddess to actively push her status to the “spare boys”, so they can do other things!

public class App {
    public static void main(String[] args) throws Exception{
        Beauty beauty = new Beauty();
        LittleBoy littleBoy = new LittleBoy();
        // Add goddess wechat
        beauty.littleBoy = littleBoy;

        // Release dynamicbeauty.publishWechat(); }}Copy the code

object-oriented

  1. If the goddess wants to add a new lick dog, she has to use her logic code.
  2. After you add a new backup, you don’t know how to share your activity with him (for example, the active method above, maybe the “new backup” doesn’t have it).
  3. What if the spare tire suddenly stopped licking? He didn’t want any more goddess updates! In this case, let’s change this code to make it “flexible” and more “object-oriented”!

V3.0

Since we want to be flexible, object-oriented. Let’s do it this way: abstract the goddess as an interface, and she needs to be able to remove the spare, add the spare, and notify the spare. At the same time, we abstracted the spare tire as an interface, and he could respond to the notification of the goddess in time! Beauty:

BeautyImpl
LittleBoyImpl

public class App {
    public static void main(String[] args) {
        Beauty beauty = new BeautyImpl();
        LittleBoy boy1 = new LittleBoyImpl("Little house");
        LittleBoy boy2 = new LittleBoyImpl("Xiao wu");

        // Add two spare tires
        beauty.addLittleBoy(boy1);
        beauty.addLittleBoy(boy2);

        // Create a circle of friends
        beauty.publishWechat("The most beautiful thing is not the rainy day, but the eaves that used to escape the rain with you!");

        // Add a new spare tire (3)
        beauty.removeLittleBoy(boy1);
        beauty.addLittleBoy(msg -> {
            System.out.println("Xiao Li: Ouch, not bad!");
        });

        // Post your moments again
        beauty.publishWechat("Tell me where the rainbow is..."); }}Copy the code

The goddess

extension

The java.util.Observer model is a published-subscribe model that is widely used in all frameworks. The JDK abstractions the observed and observed.

java.util.Obserable

conclusion

The observer pattern dynamically allows certain classes to sense changes in state and the publisher to actively notify them. Reuse interface oriented programming to reduce coupling between classes. Start from the characteristics of the observer model, through a case, step by step to improve the writing method of observation, characteristics! After the group introduced the JDK total existing implementation and its shortcomings!

Keep your eyes on me! This is dry stuff!