Observer model

The observer pattern defines a one-to-many dependency that allows multiple observers to listen on a subject object simultaneously. When the state of a topic object changes, all observer objects are notified so that they can be updated automatically.

The observer model decouple the coupling between the subject (observed) and the observer.

role

Abstract Subject, also known as an abstract Observable. It provides an interface to add and remove observer objects, abstract topic roles store all observer object references in a collection (such as an ArrayList object), and each topic can have any number of observers.

ConcreteSubject, also known as ConcreteObservable. Store status to a specific observer object, notifying all registered observers when the internal state of a specific topic changes.

The abstract Observer role (Observer) defines an interface for all concrete observers to update themselves when notified by a topic. This interface is called the update interface.

ConcreteObserver Role (ConcreteObserver), which implements the update interface required by the abstract observer role to harmonize its own state with the state of the topic. The specific observer role can maintain a reference to a specific subject object if desired.

source code

/** * Abstract theme role class */
public abstract class Subject {
    /** * Holds the registered observer object */
    private List<Observer> list = new ArrayList<Observer>();

    /** * Register the observer object **@paramObserver The observer object */
    public void attach(Observer observer){
        list.add(observer);
        System.out.println("Attached an observer");
    }

    /** * Deletes the observer object **@paramObserver The observer object */
    public void detach(Observer observer){
        list.remove(observer);
    }

    /** * Notifies all registered observer objects **@paramNewState newState */
    public void notifyObservers(String newState){
        for(Observer observer : list) { observer.update(newState); }}}Copy the code
/** * Specific theme role class */
public class ConcreteSubject extends Subject {
    private String state;

    public String getState(a) {
        return state;
    }

    public void change(String newState) {
        state = newState;
        System.out.println("The topic status is:" + state);
        // Notify observers of state changes
        this.notifyObservers(state); }}Copy the code
/** * Abstract Observer role class */
public interface Observer {
    /** * Update interface **@paramState Update status */
    public void update(String state);
}
Copy the code
/** * Specific observer role class */
public class ConcreteObserver implements Observer {

    // The state of the observer
    private String observerState;

    @Override
    public void update(String state) {
        /** * Updates the observer's state to match the target's */
        observerState = state;
        System.out.println("State is:"+ observerState); }}Copy the code
public class Client {
    public static void main(String[] args) {
        // Create a theme object
        ConcreteSubject subject = new ConcreteSubject();
        // Create an observer object
        Observer observer = new ConcreteObserver();
        // Register the observer object with the subject object
        subject.attach(observer);
        // Change the state of the subject object
        subject.change("new state"); }}Copy the code

Example – Message system

To completely separate the observer from the subject (observed), notifications can be made through the message system itself.

The message sender is called Producer, and the message receiver is called Consumer. When a Producer sends a message, he must choose which topic to send it to. Consumers can subscribe to topics they are interested in and only get specific messages.

When implementing the observer pattern using a messaging system, the Producer and Consumer are often not even on the same machine and are completely unfamiliar with each other because the action of registering the observer itself is done within the messaging system rather than within the Producer.