The observer pattern separates the Observer (subscription) from the Observer (advertisement), implements the loose coupling of the code, and defines a one-to-many dependency, allowing multiple observer colleagues to listen to an observer, and at the same time an observer can notify multiple observers of the update.
Role:
Abstract subject (Subject): Also called abstract-observer (Observable). Defines an interface that can add and remove observer objects.
Abstract Observer (Observer): Define an excuse for all the detailed observers to do the update operation.
Detailed topic (Concrete Subject): Implements an abstract topic interface that saves a reference to a list of all observer objects.
When the state changes, the observer in the notification list. Also known as detailed by the observer.
Detailed observer (concrete Observer): Implements an abstract observer interface, which is implemented in more detail by each detailed observer.
Android in development. Contentobserver,setonclicklistener. As well as your own defined interfaces (implements in activity, incoming to asynchronous tasks, when the task is finished, calling the interface function to refresh the activity page) are all very good examples of observers.
While Java itself provides support for the observer pattern, the Java.util library provides examples of the following two classes:
Observer interface, as an abstract observer. Only one interface method update () is defined, and each detailed observer is implemented separately.
The observable class, capable of being either an abstract observer or a detailed observer. Because of this class rather than the interface, the internal implementation has been able to meet the requirements of the detailed observer. Of course, you can inherit and extend this class. The observable class provides two important methods: Setchanged (), which is used to set the internal flag bit, indicating that the observed person has changed. Notifyobservers (), which is used to invoke the update () method for all observers in the list. The Observer does the update operation.
References: http://www.blogjava.net/supercrsky/articles/202544.html
Design Pattern Learning Summary--Observer pattern