Head First Design pattern reading notes (2) OBSERVER pattern Observer mode Observer pattern class diagram
Definition
Observer patterns: Define a one-to-many relationship between objects, and when one of the objects changes, all objects that depend on the object (that is, the observer) automatically update or perform certain behaviors.
Some of the original test of OO
- To deal with inter-object relationships in a loosely coupled manner –> the benefits of "Gaonezu, low coupling" when software engineering is learned
About viewer mode
- The observed object notifies the observer that a push can be used (the Notifyaction method with parameters in the class diagram), or it can use the observer's own way of obtaining updates (the Notifyaction method without parameters in the class diagram). The individual feels that the latter is more flexible, because the observed object is only responsible for notifying, without transmitting any data to the observer, and the observer is getting the data according to its own needs at the time of the update, and it can be updated asynchronously. However, the above class diagram is a bit bad, that is, the specific observer class can only inherit the observer abstract class instead of implementing the Observer interface. If you are going to implement an interface, you should change the Abstractobserver class above to an interface and change notifyaction () to Notifyaction (Abstractsubject a).
- Many of the GUI framework's event and listener mechanisms are implemented through the observer pattern.
- Java has its own observer pattern implementation, using the Java.util.Observer interface and the Java.util.Observable class.
- Depending on the implementation, the observed objects are not necessarily in the order of the list, and it is better not to have the logic to rely on this notification order elsewhere.
Head First Design pattern reading notes (2) Observer mode