Observer Mode Observer
The observer mode defines a one-to-many dependency, allowing multiple observer objects to listen to a topic object at the same time.
When the status of this topic object changes, it notifies all observer objects so that they can automatically update themselves.
Composition of observer Mode
Abstract topic role: Saves all references to the observer object in a collection. Each abstract topic role can have any number of observers. Abstract topic provides an interface to add and delete observer roles. It is generally implemented using an abstract class and interface.
Abstract observer role: Defines an interface for all specific observers and updates themselves when receiving notifications of a topic.
Specific topic roles: Send a notification to all registered observers when the internal status of a specific topic changes. A topic role is usually implemented using a subclass.
Specific observer role: This role implements the update interface required by the abstract observer role, so that its status can be consistent with that of the topic. It is usually implemented using a subclass. If necessary, a specific observer role can save a reference pointing to a specific topic role.
Code
// Abstract observer role public interface watcher {public void Update (string Str );}
Define the abstract topic role, that is, the abstract observer, in which the declaration method (add, remove observer, notify Observer ):
// Abstract topic role, watched: Observed public interface watched {public void addwatcher (watcher); Public void removewatcher (watcher); Public void policywatchers (string Str );}
Define the specific observer:
public class ConcreteWatcher implements Watcher{ @Override public void update(String str) { System.out.println(str); }}
Specific topic roles:
Import Java. util. arraylist; import Java. util. list; public class concretewatched implements watched {// stores the observer private list <watcher> List = new arraylist <watcher> (); @ override public void addwatcher (watcher) {list. add (watcher) ;}@ override public void removewatcher (watcher) {list. remove (watcher) ;}@ override public void policywatchers (string Str) {// automatic call is actually called by the topic for (watcher: List) {watcher. update (STR );}}}
Compile the test class:
Public class test {public static void main (string [] ARGs) {watched Girl = new concretewatched (); watcher watcher1 = new concretewatcher (); watcher watcher2 = new concretewatcher (); watcher watcher3 = new concretewatcher (); girl. addwatcher (watcher1); girl. addwatcher (watcher2); girl. addwatcher (watcher3); girl. notifywatchers ("happy ");}}
Observable class
The observable class is used to create subclasses that can observe other parts of your program. When the object of this subclass changes, the observation class is notified.
The observation class must implement the observer interface that defines the update () method..
The Update () method is called when an observation program is notified of a change to the observed object.
Apparently,Observable is an abstract topic object..
An object to be observed must follow the following two simple rules:
First, if it is changed, it must callSetchanged ()Method.
Second, when it is ready to notify the observer of its changes, it must callYyobservers ()Method, which leads to the call to the update () method in the observed object.
Note:: If the setchanged () method is not called before the yyobservers () method is called, no action will occur.
The yyobservers () method contains the clearchanged () method, which sets the flag variable back to the original value.
The yyobservers () method uses the Traversal method from the back to the front. That is, the added observer is called the update () method first.
Code
Defines a theme object for reciprocal counting. Each time a number changes, its observer receives this number.
One observer prints a number after receiving the notification, and the other observer starts printing when the number is smaller than or equal to 5.
Import Java. util. observable; import Java. util. observer; Class watchedcounter extends observable {public void countdown (INT number) {for (; number> = 0; -- number) {// set the variable setchanged (); // notify all observers to pass the number as the parameter information to the observer notifyobservers (number) ;}} class watcher1 implements observer {@ override public void Update (observable arg0, object arg1) {system. out. println ("watcher1's number:" + arg1) ;}} class watcher2 implements observer {@ override public void Update (observable arg0, object arg1) {If (integer) arg1 ). intvalue () <= 5) {system. out. println ("watcher2's number:" + arg1) ;}} public class observertest {public static void main (string [] ARGs) {watchedcounter = new watchedcounter (); watcher1 watcher1 = new watcher1 (); watcher2 watcher2 = new watcher2 (); // Add the observer watchedcounter. addobserver (watcher1); watchedcounter. addobserver (watcher2); // starts the Countdown count watchedcounter. countdown (10 );}}
I am the dividing line of tiantiao
Reference: http://www.cnblogs.com/mengdd/archive/2013/02/08/2909206.html
Design Pattern (13) Observer Pattern