Java Design Patterns-Observer pattern learning

Source: Internet
Author: User

Recently studied the Observer pattern in design patterns, where we recorded the results of the study.

Observer mode, personal understanding: is a one-to-many model, a subject to do things, the rest of the body can be observed. But the subject can decide who to observe and what to do to others.

The words of the master quote are

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.

The following code shows

1. New Interface Viewer Observer

/**@author*/Publicinterface  Observer    {  Public void Update (String newstate);}

The Observer object is very simple and requires an Update method that notifies the observer when the Observer is acting, that is, the Update method is triggered

2. New abstract class Observable

Importjava.util.ArrayList;Importjava.util.List; Public Abstract classObservable {//Viewer Collection    PrivateList<observer> observerlist =NewArraylist<observer>();  PublicList<observer>getobserverlist () {returnobserverlist; }     Public voidSetobserverlist (list<observer>observerlist) {         This. observerlist =observerlist; }        /*** Add Observer Object *@paramObs*/     Public voidAttach (Observer obs) { This. Observerlist.add (OBS); }        /*** Delete Observer Object *@paramObs*/     Public voidDetach (Observer obs) { This. Observerlist.remove (OBS); }        /*** Notify all observers when the action is in motion*/     Public voidnodifyobservers (String newstate) {if(NULL!=observerlist&&observerlist.size () >0){             for(Observer obs:observerlist) {obs.update (newstate); }        }    }    }

3. Create an observer's successor Observerimpl

/**@author*/Publicclass implements  Observer {    /**     * triggers           /Public when an action is taken by the Observer  void  Update (String newstate) {        System.out.println (newstate);}    }

4. Create a subclass of the Observer Observableex

 Public class extends Observable {        /**     * When the principal changes, the notification method     is called @param  newstate      *    /publicvoid change (String newstate)        {this  . Nodifyobservers (newstate);    }    }

5. Create a client

Importorg.junit.Test; Public classClient {/*** Create three new observers, add them to the list, and call the change method*/@Test Public voidTest () {Observer OBS1=NewObserverimpl (); Observer OBS2=NewObserverimpl (); Observer OBS3=NewObserverimpl (); Observableex Obse=NewObservableex ();        Obse.attach (OBS1);        Obse.attach (OBS2);                Obse.attach (OBS3); Obse.change ("I am eating"); }}

Post-run output

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

Http://www.blogjava.net/supercrsky/articles/202544.html

Java Design Patterns-Observer pattern learning

Related Article

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.