GJM: Observer mode
Video Address: http://www.imooc.com/learn/415
This course explains the application of the observer pattern in Java projects through a release and subscription case for weather forecasts. It mainly includes the structure of the observer pattern, the two implementations of the observer pattern, the model and the pull model, and when to use the observer pattern.
Observer Mode Combat
Structure and description of the Observer pattern
Learning Observer Pattern Generic code
Viewer mode
Recognize observer patterns
Two ways to implement: Push model and pull model
Using the observer implementations provided by Java
Briefly describe the pros and cons of observers
When to use Observer mode
The definition of the observer pattern
Defines a one-to-many dependency between objects. When the state of an object changes, all objects that depend on it are notified and automatically updated.
The observer receives the message The Observer observes the observer when the observer receives the message and performs its own logical processing
Structure of the Observer pattern
Subject Target Weather
SUBJECT Maintenance Observer List Subject has ADD Delete
Subject Execute Notify () = Subject Observer "Update ()" After receiving the target message
Structure diagram
subject{+add (Observer) + Delete (Observer) + Notify ()} <== ConcreteSubject [target message]
Subject =-+subject-> observer{+ Update ()} <== Concreteobserver interface logic implementation class
GJM: Design mode-Viewer mode