The "Observer Mode" of design patterns written for myself

definition

When there is a one-to-many relationship between objects, the dependent object changes, and all objects that depend on it need to be notified.

for example

Due to the impact of the epidemic, schools all over the country have delayed the start of school. The news of the delayed start is communicated by the school to the teachers and parents (regardless of the situation that the teacher forwards to the parents), and the specific start time is the same. In this example, the school has a one-to-many relationship with teachers and parents, and the school's notifications need to be notified to them. Use the observer mode to view the code.

  • First define the observed person:学校

Maintain an array of Observers, teachers and parents can register through the registerObserver method; schools publish notifications through the publishNotice method

import java.util.ArrayList;
import java.util.List;
import observer.consumer.Observer;

public class School {
    
    

    private String name;

    public School(String name) {
    
    
        this.name = name;
    }

    private List<Observer> observers = new ArrayList<>();

    public void registerObserver(Observer observer) {
    
    
        observers.add(observer);
    }

    public void publishNotice(String message) {
    
    
        System.out.println("I'm school: " + name + ", send message: " + message);
        observers.forEach(e -> e.update(message));
    }
}
  • Define the interface Observer
public interface Observer {
    
    
    void update(String message);
}
  • Define the observer: the teacher
public class Teacher implements Observer {
    
    
    private String name;

    public Teacher(String name) {
    
    
        this.name = name;
    }

    @Override
    public void update(String message) {
    
    
        System.out.println("I'm teacher: " + name + ", received message: " + message);
        // do something
    }
}
  • Define the observer: parents
public class Parent implements Observer {
    
    
    private String name;

    public Parent(String name) {
    
    
        this.name = name;
    }

    @Override
    public void update(String message) {
    
    
        System.out.println("I'm parent: " + name + ", received message: " + message);
        // do something
    }
}
  • Finally, use the main method to execute the announcement of the delayed start of school
public class Main {
    
    
    public static void main(String[] args) {
    
    
        Parent parent = new Parent("ZS");
        Teacher teacher = new Teacher("LS");

        School school = new School("CSU");
        school.registerObserver(parent);
        school.registerObserver(teacher);

        school.publishNotice("Delay the start of school");
    }
}

Insert picture description here

Pros and cons

  • advantage

    • As you can see from the above example, there is an abstract coupling between the observer and the observed. The School class does not directly call the update method of Teacher & Parent. Instead, it is interface-oriented programming and calls the method of its interface Observer. The advantage is that if you want to add a new observer, you can directly inherit the Observer class and register it to School.
    • A set of trigger mechanism has been established. If the School wants to send the scores of each student, the same mechanism can be reused, and there is no need to re-implement it.
  • Disadvantage

    • If there are too many observers, it will take a long time to execute the notification (traversing a large array), and the unnecessary observers must be unregistered in time.
    • If the observer and the observed are cyclically dependent, it may cause cyclic calls and cause the system to crash.
    • The observer does not know how the observed object changes, only the change.

Guess you like

Origin blog.csdn.net/Dkangel/article/details/107410214