This is the 25th day of my participation in the August Genwen Challenge.More challenges in August

Observer model

There is a one-to-many dependency between multiple objects, and when an object’s state changes, all dependent objects are notified and automatically updated

structure

  1. Abstract Subject role: Also called abstract target class, it provides an aggregation class for holding observer objects and methods for adding and deleting observer objects, as well as abstract methods for notifying all observers.
  2. Concrete Subject role: Also known as Concrete Target class, it implements notification methods in abstract targets to notify all registered observer objects when the internal state of a Concrete Subject changes.
  3. Abstract Observer role: It is an abstract class or interface that contains an abstract method that updates itself and is called when notified of changes to a specific topic.
  4. Concrete Observer role: Implements an abstract method defined in an abstract Observer to update its own state when it is notified of changes to the target.

demo

1. Abstract themes

public abstract class Subject {
    protected List<Observer> list = new ArrayList<>();
    public void add(Observer observer) {
        if(!this.list.contains(observer)) {
            this.list.add(observer); }}public void delete(Observer observer) {
        this.list.remove(observer);
    }
    public abstract void notifyObserver(a);
}
Copy the code

2. Specific themes

public class SubjectImpl extends Subject {
    @Override
    public void notifyObserver(a) {
        System.out.println("Theme modification!");
        for(Observer observer : list) { observer.update(); }}}Copy the code

Abstract observer

public abstract class Observer {
    public abstract void update(a);
}
Copy the code

4. Specific observer

public class ObserverOne extends Observer {
    @Override
    public void update(a) {
        System.out.println( this.getClass().getSimpleName() +  "Receive the message!"); }}public class ObserverTwo extends Observer {
    @Override
    public void update(a) {
        System.out.println( this.getClass().getSimpleName() +  "Receive the message!"); }}Copy the code

5. Client

public class Client {
    public static void main(String[] args) {
        SubjectImpl subjectImpl = new SubjectImpl();
        subjectImpl.add(new ObserverOne());
        subjectImpl.add(new ObserverTwo());
        subjectImpl.notifyObserver();
        // Theme change!
        // ObserverOne Receives messages!
        // ObserverTwo receives messages!}}Copy the code

conclusion

advantages

  1. The coupling relationship between the target and the observer is abstract. In line with the principle of dependency inversion.
  2. A set of triggers is established between the target and the observer.

disadvantages

  1. The dependency between target and observer is not completely removed, and circular references are possible.
  2. When there are many observers, the presentation of notifications takes a lot of time, affecting the efficiency of the program.

The difference between the observer model and the publish-subscribe model

Some call the observer model publish-subscribe. In my opinion, although the purpose and function are basically the same, there are some differences between the two

1. Although the observer mode also plays a role of decoupling, the decoupling is not completed, and the observed needs to actively call the observer method; There is no relationship between publishing and subscribing, and the observer is notified by a third party.

2. Observer mode: Only the observer and the observed; The publish-subscribe pattern has a message channel in addition to the observer and observed