The Observer pattern is one of the software design Patterns . In this mode, a target object manages all the observer objects that are dependent on it and proactively notifies when its own state changes. This is usually done by calling the methods provided by each observer. This mode is often used for real-time event processing systems. ----WIKIPEDIA
Personal Understanding
The observer pattern is to make the object instance of the observer in the Observer, in the event of some events, through the Notify "notify" the Observer, to complete the corresponding operation, he also called the Publish-subscribe pattern, defines a pair of multiple dependencies between objects, so that the object of the observer to generate action, You can notify the object that it depends on. The observed person has changed.
Case Analysis
Just a few days ago bought two Brazilian turtle, take this as an example, the turtle will also be hungry, he is hungry I will go to feed it to eat, such words, can say exactly with the observer pattern.
Push the way
Public interface Observable {//Add observer public void Addobserver (Observer Observer);//delete observer public void Removeobserver ( Observer Observer);//notify observer public void Notifyobservers (String info);}
Public interface Observer {//Fed public void feed (String info);
public class Turtle implements observable{private list<observer> observerlist = new arraylist<observer> (); public void Hungry () {this.notifyobservers ("I'm hungry! ");} @Overridepublic void Addobserver (Observer Observer) {observerlist.add (Observer);} @Overridepublic void Removeobserver (Observer Observer) {if (observerlist! = null && observerlist.contains ( observer)) {OBSERVERLIST.REMOVE (Observer);}} @Overridepublic void Notifyobservers (String info) {for (Observer observer:observerlist) {observer.feed (info);}}}
public class XX implements Observer {@Overridepublic void feed (String info) {System.out.println ("Feed! ");}}
The situation here is that the Turtle class knows exactly what it wants to notify the observer, so this approach can be seen as a push, but not necessarily knowing what the other person wants, and this time, it takes a pullThe way to get it is to pass an instance of itself to the observer, and the observer himself decides what to fetch, and what to pull.
Pull the way
The main changes are as follows:
public class Turtle2 implements Observable2 {private list<observer2> observerlist = new arraylist<observer2> ();p ublic void Hungry () {this.notifyobservers ();} @Overridepublic void Addobserver (Observer2 observer) {OBSERVERLIST.ADD (Observer);} @Overridepublic void Removeobserver (Observer2 observer) {if (observerlist! = null && observerlist.contains ( observer)) {OBSERVERLIST.REMOVE (Observer);}} @Overridepublic void Notifyobservers () {for (Observer2 observer:observerlist) {observer.feed (this);}} @Overridepublic Boolean isneedfeed () {return true;}}
the advantages of the Observer pattern
1. The observer and the observer are abstract coupling: it is easier to extend the observer and Observer.
2. Set up a trigger mechanism, through a series of triggering mechanism, to form a trigger chain.
the disadvantage of the Observer pattern
Multi-level triggering, the efficiency is worrying, in the design of the time to fully consider this point.
scope of application of the Observer pattern
1. Associated behavior scenarios.
2. Multi-level triggering of events.
3. Cross-system message exchange scenarios, the processing mechanism of Message Queuing.
Source code Download
Design Pattern Source Code
Recommended Reading
"Onlookers" design pattern (18)--Behavioral Template Method mode (Templatemethod pattern)
"Onlookers" design pattern (16)--the combination pattern of structure type (Composite pattern)
"Onlookers" design pattern (19)--Behavioral observer pattern (Observer pattern)