Design Pattern-Observer Pattern

Source: Internet
Author: User
The observer mode is the behavior mode of objects. It is also called the publish/subscribe mode, Model-View mode, and source-listener (Source/listener) mode) mode or dependents mode. 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 actively update themselves.
Here is a simple observer mode:
Subject. Java
Package COM. wsds. test; import Java. util. arraylist; import Java. util. list; /*** abstract topic class ** @ author administrator **/public abstract class subject {// It is used to save the object private list of the observer. <observer> List = new arraylist <observer> (); /*** note that the observer object * @ Param observer object */Public void attach (Observer observer) {list. add (observer); system. out. println ("attached an obsever");}/*** Delete the observer object * @ Param observer object */Public void detach (Observer observer) {list. remove (observer);} public void policyobservers (string newstate) {for (Observer observer: List) {observer. update (newstate );}}}
Concretesubject. Java
Package COM. wsds. test;/*** detailed topic Angle class * @ author administrator **/public class concretesubject extends subject {private string state; Public String getstate () {return state ;} public void change (string newstate) {state = newstate; system. out. println ("the topic status is:" + state); // The status changes and each observer is notified of this. yyobservers (state );}}
Observer. Java
Package com. wsds. Test; public interface observer {/*** update interface * @ Param State update status */Public void Update (string State );}
Concreteobserver. Java
Package COM. wsds. test; public class concreteobserver implements observer {// Private string observerstate of the observer; @ overridepublic void Update (string state) {/** update the Observer State, make it consistent with the target status */observerstate = State; system. out. println ("the observer's status is:" + state );}}
Client. Java
Package COM. wsds. test; public class client {public static void main (string [] ARGs) {// create a topic object concretesubject subject = new concretesubject (); // create the observer object observer = new concreteobserver (); // inject the observer object to the topic object subject. attach (observer); // change the status of the topic object subject. change ("new state ");}}
The observer mode can be divided into the push model and pull model. The above is the push mode: the PUSH model: the topic object pushes the details of the topic to the observer, regardless of whether the observer needs it or not, the pushed information is generally all or part of the data of the topic object. Pull model: When a topic object notifies the observer, it only transmits a small amount of information. Assume that the observer needs more detailed information, which is obtained from the observer to the topic object. This means that the observer pulls data from the topic object. In general, the implementation of this model will pass the topic object itself to the observer through the update () method, so that the observer can obtain the data through this reference when it needs to obtain the data. After the model is changed, the Code is as follows:
Package COM. wsds. test; import Java. util. arraylist; import Java. util. list; /*** abstract topic class ** @ author administrator **/public abstract class subject {// It is used to save the object private list of the observer. <observer> List = new arraylist <observer> (); /*** note that the observer object * @ Param observer object */Public void attach (Observer observer) {list. add (observer); system. out. println ("attached an obsever");}/*** Delete the observer object * @ Param observer object */Public void detach (Observer observer) {list. remove (observer);} public void policyobservers (string newstate) {for (Observer observer: List) {observer. update (this );}}}
Package COM. wsds. test;/*** detailed topic Angle class * @ author administrator **/public class concretesubject extends subject {private string state; Public String getstate () {return state ;} public void change (string newstate) {state = newstate; system. out. println ("the topic status is:" + state); // The status changes and each observer is notified of this. yyobservers (state );}}
Package com. wsds. Test; public interface observer {/*** update interface * @ Param State update status */Public void Update (subject );}
Package COM. wsds. test; public class concreteobserver implements observer {// Private string observerstate of the observer State; @ overridepublic void Update (subject) {/** update the Observer State, make it consistent with the target status */observerstate = (concretesubject) subject ). getstate (); system. out. println ("the observer's status is:" + observerstate );}}
Comparison between the two modes: the PUSH model assumes that the topic object knows the data required by the observer, and the PULL model assumes that the topic object does not know the detailed data required by the observer. If there is no way, simply pass itself to the observer so that the observer can take the values as needed. The push model may make it difficult to reuse the observer object. Because the update () method of the observer is the number of partitions defined as needed, it may not take into account the usage that is not taken into account. This means that when a new situation occurs, a new update () method may be provided, or the observer may be implemented again. Pulling the model will not cause this situation, the number of dimensions of the update () method is the topic object itself, which is basically the maximum data set that can be transferred by the topic object, basically able to adapt to the needs of various situations.
The observer mode is supported in Java. The observer interface and the observable interface: Observer interface: this interface only defines a method, that is, the update () method, when the State of the object to be observed changes, the notifyobservers () method of the object to be observed calls this method.
Observable interface: the observable class is a subclass of the Java. util. observable class. Java. util. observable provides public methods to support observer objects. Two of these methods are important to the observable subclass: setchanged () and notifyobservers (). The first method, after setchanged () is called, an internal tag variable is set to indicate that the status of the object to be observed has changed. The second is notifyobservers (). When this method is called, the update () method of all registered observer objects is called so that these observer objects can update themselves.
Package COM. wsds. test; import Java. util. observable;/*** detailed topic Angle class * @ author administrator **/public class concretesubject extends observable {private string state; Public String getstate () {return state ;} public void change (string newstate) {state = newstate; system. out. println ("topic status:" + state); // tag the status change setchanged (); // The status changes, notifying the various observers of this. yyobservers (state );}}
Package COM. wsds. test; import Java. util. observable; import Java. util. observer; public class concreteobserver implements observer {// observer status private string observerstate; Public concreteobserver (observable O) {o. addobserver (this) ;}@ overridepublic void Update (observable o, object Arg) {/** update the Observer State to make it consistent with the target State */observerstate = (concretesubject) o ). getstate (); system. out. println ("the observer's status is:" + observerstate );}}




Design Pattern-Observer Pattern

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.