May 27, 2021 Design mode
Observer Pattern is used when there is a one-to-many relationship between objects. F or example, when an object is modified, it is automatically notified of its dependent objects. The observer pattern is a behavioral pattern.
Intent: Defines a one-to-many dependency between objects, and when the state of an object changes, all objects that depend on it are notified and automatically updated.
Key solution: One object state changes to notify other objects, and to take into account ease of use and low coupling, to ensure a high degree of collaboration.
When to use: The state of an object (target object) changes, and all dependent objects (observer objects) are notified and broadcast.
How to solve it: This dependency can be weakened using object-oriented techniques.
Key code: There is an ArrayList in the abstract class that holds the observer.
Application example: 1, when the auction, the auctioneer observes the highest bid price, and then informs the other bidders to bid. 2 , the western traveloda inside Wukong asked Odessa to serve the red child, Odessa sprinkled a water to attract an old turtle, this turtle is the observer, he observed the action of the Odessa sprinkle water.
Pros: 1, the observer and the observed are abstractly coupled. 2 , the establishment of a set of trigger mechanism.
Cons: 1, if an observer object has many direct and indirect observers, notifying all observers will take a lot of time. 2 . If there is a circular dependency between the observer and the observer target, the observation target triggers a circular call between them, which may cause the system to crash. 3 . The observer pattern does not have a mechanism to let the observer know how the object under observation has changed, but only that the target has changed.
Use scenario: 1, there are multiple sub-classes common methods, and the logic is the same. 2, important, complex methods, can be considered as a template method.
Note: 1, JAVA already has a support class for observer patterns. 2 , avoid circular references. 3 , if the order of execution, an observer error will lead to the system casing, generally asynchronous way.
Observer mode uses three classes of Subject, Observer, and Client. S ubject objects have a binding observer to the Client object and a way to untie the observer from the Client object. We created the Subject class, the Observer abstract class, and the entity class that extended the abstract class Observer.
ObserverPatternDemo, our demo class uses Subject and entity class objects to demonstrate observer patterns.
Create an Subject class.
Subject.java
import java.util.ArrayList; import java.util.List; public class Subject { private List<Observer> observers = new ArrayList<Observer>(); private int state; public int getState() { return state; } public void setState(int state) { this.state = state; notifyAllObservers(); } public void attach(Observer observer){ observers.add(observer); } public void notifyAllObservers(){ for (Observer observer : observers) { observer.update(); } } }
Create an Observer class.
Observer.java
public abstract class Observer { protected Subject subject; public abstract void update(); }
Create an entity observer class.
BinaryObserver.java
public class BinaryObserver extends Observer{ public BinaryObserver(Subject subject){ this.subject = subject; this.subject.attach(this); } @Override public void update() { System.out.println( "Binary String: " + Integer.toBinaryString( subject.getState() ) ); } }
OctalObserver.java
public class OctalObserver extends Observer{ public OctalObserver(Subject subject){ this.subject = subject; this.subject.attach(this); } @Override public void update() { System.out.println( "Octal String: " + Integer.toOctalString( subject.getState() ) ); } }
HexaObserver.java
public class HexaObserver extends Observer{ public HexaObserver(Subject subject){ this.subject = subject; this.subject.attach(this); } @Override public void update() { System.out.println( "Hex String: " + Integer.toHexString( subject.getState() ).toUpperCase() ); } }
Use Subject and entity observer objects.
ObserverPatternDemo.java
public class ObserverPatternDemo { public static void main(String[] args) { Subject subject = new Subject(); new HexaObserver(subject); new OctalObserver(subject); new BinaryObserver(subject); System.out.println("First state change: 15"); subject.setState(15); System.out.println("Second state change: 10"); subject.setState(10); } }
Verify the output.
First state change: 15 Hex String: F Octal String: 17 Binary String: 1111 Second state change: 10 Hex String: A Octal String: 12 Binary String: 1010