Java Watcher Pattern

Source: Internet
Author: User

The Observer pattern is the behavior pattern of the object, also called the Publish-subscribe (publish/subscribe) mode, the Model-view (Model/view) mode, the source-listener (Source/listener) mode, or the slave (dependents) mode.

The Observer pattern defines a one-to-many dependency that allows multiple observer objects to listen to a Subject object at the same time. When the subject object changes in state, all observer objects are notified so that they can automatically update themselves.

In the observer mode, it is divided into two ways: push model and pull model.

Push model

The subject object pushes the details of the topic to the observer, and the information that is pushed is usually all or part of the data of the subject object, regardless of whether or not the observer needs them.

Pull model

The Subject object transmits only a small amount of information when it notifies the observer. If the observer needs more specific information, the Observer takes the initiative to get to the subject object, which is equivalent to the observer pulling data from the subject object. In the general implementation of this model, the subject object itself is passed to the observer through the update () method, which can be obtained by this reference when the observer needs to obtain the data.

Push mode:

 PackageCom.qhong;Importjava.util.ArrayList;Importjava.util.List; Public classMain { Public Static voidMain (string[] args) {//Create a Theme objectConcreteSubject subject =NewConcreteSubject (); //To create an observer objectObserver Observer =NewConcreteobserver (); //register the Observer object on the Subject ObjectSubject.attach (Observer); //change the state of a Subject objectSubject.change ("New state"); }}InterfaceObserver {/*** Update interface *@paramstatus of State update*/     Public voidUpdate (String state);}classConcreteobserverImplementsObserver {//the state of the Observer    PrivateString observerstate; @Override Public voidUpdate (String state) {/*** Update the Observer's state so that it is consistent with the status of the target*/observerstate=State ; System.out.println ("Status is:" +observerstate); }}Abstract classSubject {/*** Used to save the registered observer object*/    Privatelist<observer> list =NewArraylist<observer>(); /*** Registered Observer Object *@paramObserver Observer Object*/     Public voidAttach (Observer Observer) {list.add (Observer); System.out.println ("Attached an observer"); }    /*** Delete Observer Object *@paramObserver Observer Object*/     Public voidDetach (Observer Observer) {list.remove (Observer); }    /*** Notify all registered Observer objects*/     Public voidnodifyobservers (String newstate) { for(Observer observer:list) {observer.update (newstate); }    }}classConcreteSubjectextendssubject{PrivateString State;  PublicString getState () {returnState ; }     Public voidChange (String newstate) { state=newstate; System.out.println ("Subject status is:" +State ); //changes in status, notifying individual observers         This. Nodifyobservers (state); }}
attached an observer theme status: New state status is: New State

Pull mode:

 PackageCom.qhong;Importjava.util.ArrayList;Importjava.util.List; Public classMain { Public Static voidMain (string[] args) {//Create a Theme objectConcreteSubject subject =NewConcreteSubject (); //To create an observer objectObserver Observer =NewConcreteobserver (); //register the Observer object on the Subject ObjectSubject.attach (Observer); //change the state of a Subject objectSubject.change ("New state"); }}InterfaceObserver {/*** Update interface *@paramsubject incoming Subject object, aspect gets the state of the corresponding Subject object*/     Public voidUpdate (Subject Subject);}classConcreteobserverImplementsObserver {//the state of the Observer    PrivateString observerstate; @Override Public voidUpdate (Subject Subject) {/*** Update the Observer's state so that it is consistent with the status of the target*/observerstate=((ConcreteSubject) subject). GetState (); System.out.println ("Observer status:" +observerstate); }}Abstract classSubject {/*** Used to save the registered observer object*/    Privatelist<observer> list =NewArraylist<observer>(); /*** Registered Observer Object *@paramObserver Observer Object*/     Public voidAttach (Observer Observer) {list.add (Observer); System.out.println ("Attached an observer"); }    /*** Delete Observer Object *@paramObserver Observer Object*/     Public voidDetach (Observer Observer) {list.remove (Observer); }    /*** Notify all registered Observer objects*/     Public voidnodifyobservers () { for(Observer observer:list) {observer.update ( This); }    }}classConcreteSubjectextendssubject{PrivateString State;  PublicString getState () {returnState ; }     Public voidChange (String newstate) { state=newstate; System.out.println ("Subject status is:" +State ); //changes in status, notifying individual observers         This. Nodifyobservers (); }}

Comparison of the two modes
The push model is the assumption that the subject object knows the data that the observer needs, whereas the pull model is a subject object that does not know what data the observer specifically needs, and if there is no way, simply pass it on to the observer, allowing the observer to take the value on its own.
Pushing a model may make it difficult to reuse the observer object because the Observer's update () method is a parameter that is defined as needed, and may not be able to take into account unused usage. This means that a new update () method may be available when a new situation arises, or simply a re-implementation of the observer, whereas the pull model does not cause this, since the parameters of the update () method under the pull model are the subject object itself, which is basically the largest collection of data that the subject object can deliver. Can basically adapt to the needs of various situations.

Http://www.cnblogs.com/java-my-life/archive/2012/05/16/2502279.html

Java Watcher 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.