Observer pattern (Observer pattern): Defines a one-to-many dependency between objects, and when an object's state changes, all objects that depend on it are notified and automatically updated. Also called publish-subscribe mode
(i) Why observer patterns are required
1, an object state changes to other object notification issues, and to consider the ease of use and low coupling, to ensure a high degree of collaboration
2, the perfect separation between the observer and the observed object, so that each class will focus on a certain function, an object to do only one thing.
3, the Observer pattern defines a clear boundary between the modules, improving the maintainability and reusability of the application.
(ii) observer mode UML diagram
(c) Simple examples
The observer pattern is also called the Publish subscription pattern, if we are doing a system now. We let all the clients subscribe to our service, then notify the client to update when there is update information on our server. The server here is the Observer, and the client is the observer.
<?phpAbstract by ObserverAbstractClasssubject{Defines an array of observersPrivate $observers =Array ();Adding observer methodsPublicfunctionAddobserver(Observer $observer) {$this->observers[] = $observer;Echo"Add observer Success". Php_eol; }Remove Observer methodPublicfunctionDelobserver(Observer $observer) {$key = Array_search ($observer,$this->observers);Determine if there is a presence of the observerif ($observer = = =$this->observers[$key]) {Although the values are the same but may not be the same object, use congruent judgmentunset$this->observers[$key]);Echo' Delete viewer success '. Php_eol; }else{Echo' Observers do not exist, no need to delete '. Php_eol; } }Notify all observersPublicfunctionNotifyobservers(){foreach$this->observersAs $observer) {$observer->update ();}}}Specific viewer service sideClassServerExtendssubject{The specific observer business publishes a message and notifies all clientsPublicfunctionPublish(){Echo' It's a nice day, I released the update package '. Php_eol;$this->notifyobservers (); }}Abstract Observer InterfaceInterfaceobserver{PublicfunctionUpdate();}Specific Observer classEndClassWechatImplementsobserver{PublicfunctionUpdate(){Echo' Notification received, update complete '. Php_eol; }}Web-sideClassWebImplementsobserver{PublicfunctionUpdate(){Echo' Notify received, Web-side system update '. Php_eol; }}App sideClassAppImplementsobserver{Publicfunctionupdate () {echo notification received, app end updated later. Php_eol; }}//instantiation of the Observer $server = new server; new wechat; $web = new web; $app = new App; //Add the Observer $server->addobserver ($wechat); $server->addobserver ($web); $server Addobserver ($app); //by the Observer publishes the Information $server->publish (); //Delete Observer $server->delobserver ($wechat); //again release Information $server->publish (); //attempt to delete an object that has not been added as an observer $server->delobserver (new Web); //re-release information $server->publish ();
A key word of the observer pattern is the trigger, and the observer's action triggers the Observer's response. Another common area of observer patterns is the plug-in system.
Another way to implement the observer in PHP is by implementing the Splsubject interface and Splobserver.
PHP Implementation Viewer mode