What is the Observer pattern?
What is the Observer pattern? The Observer pattern defines a one-to-many dependency between objects, so that when an object changes state, all its dependents are notified and updated automatically.
Here, the object of change is called the observation target, and the object being notified is called the Observer. An observation target can correspond to multiple observers, and these observers are not interconnected, so you can add and remove observers as needed, making the system easier to scale.
Observer mode, also known as publish-subscribe mode
What are the advantages of the Observer pattern?
1, it's a good solution to the problem of coupling in a one-to-many dependency, so that when the state of an object changes, all objects that depend on it are notified and refreshed automatically.
2, when you want to add a dependent object, do not change one end of the code.
When do you use Observer mode?
1, this mode is usually used to implement the event processing system. (You sent a dynamic dynamic to the QQ to be sent to the person can see, add Group is registered, back Group is notify)
2, there is a better understanding, when one thing is done, you can do other things.
such as: With Money can buy food, wear, drink. (One-to-many). When you spend money in the bank will be, SMS notify you, email notify you, notify you, you can also cancel SMS notification.
What are the points of note when using Observer mode?
If there is a cyclic dependency between the observers, the observed will trigger a cyclic call between them, causing the system to crash. It is important to pay special attention to this when using the Observer pattern.
How to design the observer pattern, how to do?
A very good understanding of a blog
http://blog.csdn.net/chenssy/article/details/8955696
Java built-in observation mode
Observable: is a class used when subclasses.
1, this class to implement the registration, notification, removal of three methods
2, notifications are available in two ways (two methods notifyobservers () and notifyobservers(Object Arg))
3, call the setchanged() before calling the two notification method;
Observer: There is an Update method inside the interface
Java Design Pattern 2-viewer mode