Interpretation of Observer Design Pattern

The observer mode is a mode with a very high usage rate. It is commonly used in GUI systems and subscription-publishing systems. Because an important function of this model is decoupling, decoupling the observer and the observer, making the dependence between them even smaller, or even without dependence.

definition

Define a one-to-many dependency relationship between objects, so that when each object changes state, all objects that depend on it will be notified and automatically updated.

UML graphics

Observer UML

  • The Subject
    abstract topic, that is, the role of the Observable. The abstract topic role stores references to all observer objects in a collection. Each topic can have any number of observers. The abstract topic provides an interface. Add and delete observer objects
  • ConcreteSubject
    specific subject, this role saves the relevant state into the specific observer object, and when the internal state of the specific subject changes, it will notify all registered observers. The specific subject role is also called the concrete observer (ConcreteObservable) role
  • Observer is an
    abstract observer. This role is an abstract class of observers. It defines an update interface to update itself when notified of changes in the theme
  • ConcreteObserver
    concrete observer. This role implements the update interface defined by the abstract observer role in order to update its own state when the state of the subject changes

Observer mode code implementation (Java)

Abstract Observed


public abstract class Subject {

    // 定义一个观察者数组
    private Vector<Observer> obs = new Vector<>();

    // 增加一个观察者
    public void addObserver(Observer o) {
        this.obs.add(o);
    }

    // 删除一个观察者
    public void delObserver(Observer o) {
        this.obs.remove(o);
    }

    // 通知所有观察者
    public void notifyObservers() {
        for (Observer o : this.obs) {
            o.update();
        }
    }

    public abstract void doSomeThing();

}

Specific observer

public class ConcreteSubject extends Subject{
    
    

    @Override
    public void doSomeThing() {
        // TODO 被观察者 处理具体业务逻辑
        System.out.println("被观察者处理业务逻辑 ,并通知观察者");
        // 通知观察者 
        super.notifyObservers();
    }

}

Observer

public interface Observer {
    // 更新方法
    public void update();
}

Specific observer

public class ConcreteObserver implements Observer{
    
    
    @Override
    public void update() {
        // 观察者处理具体业务逻辑
        System.out.println("观察者接到信息,并处理");  
    }
}

Observer mode call

public class ObserverPatterns {

    public static void main(String[] args) {
        ConcreteSubject observed = new ConcreteSubject();
        Observer observer=new ConcreteObserver();
        observed.addObserver(observer);
        observed.doSomeThing();
    }
}

Applicable scene

  1. Associated behavior scenario
  2. Event multi-level departure scene
  3. Cross-system message exchange scenarios, such as message queue and event bus processing mechanism

to sum up

The main function of the observer pattern is to decouple the object, completely isolate the observer from the observed, and only rely on the Observer and Observable abstractions

advantage

  • There is an abstract coupling between the observer and the observed to deal with business changes
  • Enhance the flexibility and scalability of the system

Disadvantage

When applying the observer mode, you need to consider the development efficiency and operating efficiency issues. The program contains one observer, multiple observers, development and debugging, etc. content will be more complicated, and the notification of messages in JAVA is executed sequentially by default. An observer is stuck, which will affect the overall execution efficiency. In this case, the asynchronous method is generally used

Guess you like

Origin blog.csdn.net/xufei5789651/article/details/79937870