“This is the fifth day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

Behavioral pattern is to describe the interaction between objects and interaction methods

An overview of the

What is the observer model

Defines a one-to-many dependency between objects in which all dependent objects are notified and automatically updated when an object’s state changes.

When to use observer mode

Broadcast notifications are used when a one-to-many relationship exists between objects and changes to the current object need to be notified to other dependent objects.

example

An example from the Rookie tutorial is very accurate: “During an auction, the auctioneer observes the highest bid and notifies other bidders to bid.” The auctioneer was an observer

In fact, we use MQ on a regular basis, also carry this idea. A change in a value notifies programs that depend on it

implementation

Logic implementation

The name in the parents’ account book is changed, and the information in the “father” column of the children’s account book is also changed

  1. Create a parent class with name, childrens, and notifyAllObservers() for notifying children.
  2. Create observer with parent,abstract void update(); ParentName method
  3. Implementation of children information, which update method to observe the update logic

Code implementation

  1. Implement the parent class, noting that all of its dependencies need to be notified after changing the name
public class Parent {
    /** * the point on which other classes depend */
    private List<Children> childrens
            = new ArrayList<Children>();
    private String name;
    public String getName(a) {
        return name;
    }
    public void setName(String name) {
        this.name = name;
        notifyAllObservers();
    }
    public void attach(Children children){
        childrens.add(children);
    }
    public void notifyAllObservers(a){
        for(Children children : childrens) { children.update(); }}}Copy the code
  1. Abstract methods for implementing children and children
public abstract class Children {

    protected Parent parent;
    public abstract void update(a);
}

public class Daughter extends Children {

    /** * Monitoring parameters */
    private String parentName;

    public Daughter(Parent parent) {
        // When implementing this object, it depends on the Parent object
        this.parent = parent;
        this.parent.attach(this);
    }

    @Override
    public void update(a) {
        // Handle the logic of observing changes
        this.parentName = parent.getName();
        System.out.println("i'm daughter ,my Father name is : "+ parent.getName()); }}public class Son extends Children {

    /** * Monitoring parameters */
    private String parentName;

    public Son(Parent parent) {
        // When implementing this object, it depends on the Parent object
        this.parent = parent;
        this.parent.attach(this);
    }

    @Override
    public void update(a) {
        // Handle the logic of observing changes
        this.parentName = parent.getName();
        System.out.println(" i'm son ,my Father name is : "+ parent.getName()); }}Copy the code
  1. Test the implementation.

public class ObServerDemo {


    public static void main(String[] args) {
        Parent parent = new Parent();
        new Son(parent);
        new Daughter(parent);

        parent.setName("Li gang");
        parent.setName("Wang gang"); }} Output: I'M son,my Father name is: Li Gang I'M Daughter,my Father name is Li Gang'M son,my Father name is: Wang Gang I'M Daughter,my Father name is Wang GangCopy the code

This shows that the circular notification operation, if too many dependencies, will result in too much logic for related notification. So this logic can be optimized to make the process of finding a notification asynchronous and open a separate thread for processing (again designing data synchronization and locking).