This is the 7th day of my participation in the August Gwen Challenge.

The observer pattern refers to a one-to-many dependency between objects in which all dependent objects are notified and automatically updated whenever that particular object changes state.

In layman’s terms, the observer model is actually the same as the newspaper subscription model. A newspaper has two main roles: publisher and subscriber. The publisher publishes the newspaper and the subscriber subscribes to the publisher. When the subscriber subscribes to the newspaper, the publisher will notify and push to the subscriber every time the newspaper is published.

In the observer model, the publisher is called the observed and the subscriber is called the observer.

The model structure

As shown in the figure, the Observer mode is mainly composed of two roles: Subject and Observer.

  • Subject (Subject): exists in the form of an interface, needs to be implemented by the actual observed (Concrete Subject), maintains a list of observers, and hasattach().detach().notify()Three ways. Among themattach()anddetach()Add and remove observers, respectively.notify()Responsible for calling the observerupdate()Method for notifications and status updates.
  • An Observer: exists in the form of interface and needs to be implemented by Concrete Observerupdate()A method that is provided to be called by the observer for notifications and status updates.

Push model and pull model

The observer mode can be divided into push model and pull model according to the way of data push.

  • Push model: After the data of the observed is updated, it will notify the observer and actively push the data, whether the observed needs it or not.
  • Pull model: After the data of the observed is updated, it notifies the observer, but does not actively push the data. When the observer needs data, it can actively pull data from the observed.

Push model and pull model are more semantic and logical differences. In the process of code implementation, it can be written in the form of general parameters supporting both models, which can be specifically seen in the Observer interface and Observable class under the java.util package.

The specific implementation

Take the newspaper subscription scenario as an example, there is one observer: the newspaper and two observers: individuals and enterprises. The concrete implementation is as follows:

Class diagram:

Observed:

/** * public interface Subject {void attach(Observer Observer); void detach(Observer observer); void notifyObserver(); } /** * public class Newspaper implements Subject {private List<Observer> observerList; private String newspaperContent; public Newspaper(){ observerList = new ArrayList<Observer>(); } @Override public void attach(Observer observer) { observerList.add(observer); } @Override public void detach(Observer observer) { observerList.remove(observer); } @Override public void notifyObserver() { for (int i = 0; i < observerList.size(); i++) { observerList.get(i).update(newspaperContent); } } public String getNewspaperContent() { return newspaperContent; } public void setNewspaperContent(String newspaperContent) { this.newspaperContent = newspaperContent; }}Copy the code

Observer:

/** * Public interface Observer {void update(String newspaperContent); */ public class People implements Observer{private String name; private String newspaperContent; public People(String name) { this.name = name; } @Override public void update(String newspaperContent) { this.newspaperContent = newspaperContent; System.out.println(" I am "+this.name+", I received the newspaperContent :"+this.newspaperContent); */ public class Company implements Observer{private String name; private String newspaperContent; public Company(String name) { this.name = name; } @Override public void update(String newspaperContent) { this.newspaperContent = newspaperContent; System.out.println(this.name+" received newspaperContent "); }}Copy the code