Design mode: Viewer mode (publish-subscribe (publish/subscribe) mode)

Source: Internet
Author: User

Observer pattern Definition: defines a one-to-many dependency between objects, and when the state of the object changes, all objects that depend on it are notified and automatically updated (note: Dependency here is an aggregated approach). The author of these definitions of what is actually quite not cold, although this write really very good look. Below the code to run directly, by the ' boys look beautiful shower ' example, to discuss the next mode.

The code is affixed:

<?PHP//The object to which the interface is implemented by the caller interface can be seen as actually being looked at by the sisterInterfaceibeatifulgirl{ Public function Join(Iboy$boy);//Add a Lookout         Public functionGoaway (Iboy$boy);//kill the Lookout .         Public functionNotify ();//notify the Lookout    }//a beautiful sister to be looked at .classBeatifulgirlImplementsibeatifulgirl{Private $boys;//define the lookout set (here the lookout has served, sister has news will be notified.) )        function__construct () {$this->boys =Array();//Initializing a collection    }        //Add a lookout (otherwise how to notice it?) )     Public   function Join(Iboy$boy){              if(!In_array($boy->name,Array_keys($this-Boys)))  {                      $this->boys[$boy->name]=$boy; }    }        //Kill the Lookout (was found by the sister, or is too ugly, not allowed to hit the lookout.) )     Public functionGoaway (Iboy$boy){         if(In_array($boy->name,Array_keys($this-Boys)))  {                  unset($this->boys[$boy-name]); }    }        //inform the lookout, tell them to get ready.     Public functionNotify () {foreach($this->boys as $boy){            $boy-update (); }    }        //the beautiful girl who was looking down began to shower.     Public functionshower () {Echo' The beauty begins to shower <br/> '; //notify all the lookout         $this-notify (); }}//The caller interface implements the object of the interface can be regarded as the actual person to lookInterfaceiboy{ Public functionUpdate ();//when you receive the news from your sister, prepare yourself immediately, you know.     }//Zhang Sanlai's looking.classZhangsanImplementsiboy{ Public $name; function__construct ($name) {            $this->name =$name; }        //Zhang San receive notification of behavior: may be too far away, so prepare the telescope in advance.      Public functionUpdate () {Echo $this->name. ' Received: Prepare the telescope and start looking. <br/> '; }}//Li Shilai's looking.classLisiImplementsiboy{ Public $name; function__construct ($name) {            $this->name =$name; }        //John Doe The Act of receiving a notification: a rare opportunity to have a camera ready.      Public functionUpdate () {Echo $this->name. ' Received: Prepare the camera and start taking pictures. <br/> '; }}//Wang Guolai's looking.classWangwuImplementsiboy{ Public $name; function__construct ($name) {            $this->name =$name; }        //Harry received notification behavior: Do this young a big move, the camera record down.      Public functionUpdate () {Echo $this->name. ' Received: Prepare the camera and start shooting. <br/> '; }}//There 's a sister in the bathroom.$girl=NewBeatifulgirl ();$girl-Join(NewZhangsan (' Zhang San '));//Zhang three wants to make a lookout$girl-Join(NewLisi (' John Doe '));//li Si to look at$girl-Join(NewWangwu (' Harry '));//Harry to look//beauty start shower benefits come!!! $girl-shower ();EchoHappy I'm drunk, too! ‘;

Attach Baidu's Observer mode generic class diagram. The girl in the shower is a specific observer, Dick and Harry Harry is a specific observer.

Pros: Loose coupling between objects, you can interact, don't know each other's details. The observer can aggregate multiple observers and notify them all, and do not care what the observer will do or what to do when the notice is given. (Anyway, I began to shower, you do not come to the onlookers), support ' broadcast communications ', and comply with the ' open and close principle ' requirements.

Cons: If the Observer has a lot of observers, the notification may be a lot of time, and if the observer and the observer are cyclic dependent, it can cause the system to crash; In the case of multiple observers, an observer may cause a long or unresponsive notification response time. (This can take into account the asynchronous architecture or leave sufficient resources to do the synchronization architecture)

Use occasions: as defined, when an object state changes, it can be used as a scenario that causes changes in the state of the various parties.

The first time I wrote a blog, a little foreign flavor feeling. However, due to the limited level, if there are problems, we discuss together.

Contact information:

Name: 24K Pure Cock Silk

qq:1418197673

Email:[email protected]

Design mode: Viewer mode (publish-subscribe (publish/subscribe) mode)

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.