Observer model

& mesp; In observer mode, one observedmanagementAll observer objects that are attached to it and change their stateUnsolicited notification. This is usually done by calling the methods provided by each observer. This pattern is often used to implement event handling systems.

role

Abstract the observed role: Keeps all references to the observer object in a collection, and each observed role can have any number of observers. The observed provides an interface to add and remove observer roles. It is typically implemented with an abstract class and interface. – Provides addWatcher, removerWatcher, and notifyWatchers methods. – There is a variable in the observed object that stores the observed object. Abstract Observer roles: Define an interface for all concrete observers to update themselves when notified of a topic. Specific role of the observed: Notifies all registered observers of internal state changes. The observed role is usually implemented as a subclass.

// Notification is sent to all registered observers when the internal state of the observed changes, and the notification is carried out through the method provided by the observer. The observer receives notification and updates its status. @Override public void notifyWatchers() { for (Watcher watcher : list) { watcher.update(); }}Copy the code

Concrete observer role: This role implements the update interface required by the abstract observer role to harmonize its own state with that of the topic. It is usually implemented as a subclass. If desired, the specific observer role can save a reference to the specific subject role.

Applicable scenario

1) When an abstract model has two aspects, one of which depends on the other. Encapsulate the two in separate objects so that they can be changed and reused independently. 2) When changes to one object require changes to other objects without knowing exactly how many objects need to be changed. 3) When an object must inform other objects, and it cannot assume who the other objects are. In other words, you don’t want these objects to be tightly coupled.

Application: Armored car transportation

Abstract the Watched role: Watched

/** Observer */ public Interface Watched {/** * The Watched can add observers * @param watcher */ public void addWatcher(Watcher watcher); @param watcher */ public void removerWatcher(watcher watcher); Public void notifyWatchers(); public void notifyWatchers(); }Copy the code

Specific role to be observed: Armored truck CrushTruck

The armored car is the observed object, and it is observed according to the abstract role. The observer holds a list to store the observers that depend on it, which is easy to manage. Add and remove observers through addWatcher() and removeWatcher(), but the state changes inside the armored car. Notifying all managed observers with notifyWaters() is actually notifying the observers with update() via methods provided by the observers

import java.util.ArrayList; import java.util.List; /** public class CrushTruck implements Watched{private List<Watcher> List = new ArrayList<Watcher>(); @Override public void addWatcher(Watcher watcher) { list.add(watcher); } @Override public void removerWatcher(Watcher watcher) { list.remove(watcher); } @Override public void notifyWatchers() { for (Watcher watcher : list) { watcher.update(); }}}Copy the code

Abstract observer: Simply provides a method to receive changes in the observer’s state and update its own state.

Observable Observable Observable Observable Observable Observable Observable Observable Observable Observable Observable Observable Observable Observable Observable Observable Observable Observable Observable Observable Observable Observable Observable Observable Observable Observable Observable Observable Observable Observable Observable Observable Observable Observable Observable Observable Observable Observable Observable Observable Observable Observable Observable Observable Observable Observable Observable Observable Observable Observable Observable }Copy the code

Concrete observer

Public class Security implements Watcher{@override public void update(){system.out.println (" implements Security ", "implements Security "); }} public class implements Watcher{@override public void update() {system.out.println (" implements "); ); }} public class implements Watcher{@override public void update() {system.out.println (" implements Watcher; After doing this..." ); }}Copy the code