PHP Watcher Mode

Source: Internet
Author: User

The Observer pattern (sometimes called the Publish/Subscribe mode ) is one of the software design patterns. In this mode, a target object manages all the observer objects that are dependent on it and proactively notifies when its own state changes. This is usually done by calling the methods provided by each observer. This pattern is often used to implement event handling systems. (Wikipedia)

Example of a PHP observer pattern:

1<?PHP2 /**3 * Auth Phpzhou4 * Date 2015-4-175 */6 Interfacesubject{7      Public functionRegister (Observer$o);8      Public functionRemove (Observer$o);9      Public functionnotify ();Ten } One  A Interfaceobserver{ -      Public functionupdate (); - } the  - //Specific Topics - classPaperImplementssubject{ -     Private $_class=Array(); +      Public functionRegister (Observer$o){ -         $this->_class[] =$o; +     } A      Public functionRemove (Observer$o){ at         if(!Empty($this-_class)) { -             foreach($this->_class as $key=$observer){ -                 if($observer==$o){ -                     //unset ($this->_class[$key]); -                     Array_splice($this->_class,$key, 1); -                 } in             } -              to         } +     } -      Public functionNotify () { the         if(!Empty($this-_class)) { *             foreach($this->_class as $observer){ $                 $observer-update ();Panax Notoginseng             } -         } the     } + } A  the //an observer + classCurrentobserverImplementsobserver{ -      $      Public functionUpdate () { $         Echo"Currentobserver good!",Php_eol; -     } - } the  - //an observerWuyi classNextobserverImplementsobserver{ the      Public functionUpdate () { -         Echo"Nextobserver good!",Php_eol; Wu     } - } About  $ //Test - $paper=NewPaper (); - $paper->register (Newcurrentobserver ()); - $paper->register (Newnextobserver ()); A $paper-notify (); + $paper->remove (Newnextobserver ()); the Echo"<br>"; - $paper->notify ();

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