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