When there is a one-to-many relationship between objects, the Observer Pattern is used. For example, when an object is modified, it will automatically notify its dependent object. The observer mode is a behavioral mode.
Intent: defines a one-to-many dependency between objects. When an object's state changes, all objects that depend on it are notified and automatically updated.
Main solution: An issue where object state changes are notified to other objects, and easy to use and low coupling are considered to ensure a high degree of collaboration.
When to use: The state of an object (target object) changes, and all dependent objects (observer objects) will be notified and broadcasted.
How to solve: Using object-oriented technology, this dependency can be weakened.
Key Code: There is an ArrayList in the abstract class that holds the observers.
Application example: 1. At the time of the auction, the auctioneer observes the highest price and then informs other bidders to bid. 2. In the Journey to the West, Goku asked Bodhisattva to surrender to the red baby. The Buddha sprinkled a water to recruit an old turtle. This turtle is an observer. He observes the action of the Buddha sprinkling water.
Advantages: 1. The observer and the observed person are abstractly coupled. 2. Establish a trigger mechanism.
Disadvantages: 1. If an observed object has many direct and indirect observers, it will take a lot of time to notify all observers. 2. If there is a circular dependency between the observer and the observed target, the observation target will trigger a circular call between them, which may cause the system to crash. 3. Observer mode There is no corresponding mechanism for the observer to know how the observed target object has changed, but only to know that the observation target has changed.
- An abstract model has two aspects, one of which depends on the other. These aspects are encapsulated in separate objects so that they can be changed and reused independently of each other.
- An object change will cause other one or more objects to change as well, without knowing how many objects will change, which can reduce the degree of coupling between objects.
- An object must notify other objects without knowing who they are.
- You need to create a trigger chain in the system. The behavior of the A object will affect the B object. The behavior of the B object will affect the C object.... You can use the observer mode to create a chain trigger mechanism.
Note: 1. There is already a support class for observer mode in JAVA. 2. Avoid circular references. 3. If the order is executed, an observer error will cause the system to jam, generally in an asynchronous manner.
Observer mode uses three classes, Subject, Observer, and Client. The Subject object has methods for binding the observer to the Client object and unbinding the observer from the Client object. We create the Subject class, the Observer abstract class, and the entity class that extends the abstract class Observer.
ObserverPatternDemo, our demo class uses Subject and entity class objects to demonstrate the observer pattern.
Create a Subject class.
Create the Observer class.
Create an entity observer class.
Use Subject and entity observer objects.
Executing the program, outputting results:
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