PHP design pattern-Observer pattern. PHP design pattern-Observer pattern (sometimes referred to as publish-subscribe pattern ). In this mode, a target object manages all observer objects dependent on it. in PHP design mode, the observer mode.
Observer Mode (sometimes referred to as publish-subscribe mode ). In this mode, a target object manages all observer objects that depend on it and actively sends notifications when its status changes. This is usually done by calling the methods provided by various observers. This mode is usually used to implement the event processing system.
UML class diagram:
Important roles:
Abstract notifier role (INotifier): defines the notification interface rules.
Specific notification owner role (Boss): implements the abstract notification owner interface and immediately sends a notification to the observer when the status changes.
Abstract observer role (IObserver): defines the operation (Update) interface rules after the notification is received.
Specific observer role (JingDong): the specific operation method.
Core code:
// Abstract notifier abstract class Subject {private $ observers = array (); public function Attach (Observer $ observer) {array_push ($ this-> observers, $ observer );} public function Detach (Observer $ observer) {foreach ($ this-> observers as $ k => $ v) {if ($ v = $ observer) {unset ($ this-> observers [$ k]) ;}} function between Y () {foreach ($ this-> observers as $ v) {$ v-> Update () ;}}// the specified Notifier (Boss and Secretary) class ConcreteSubject extends Subject {public $ subject_state ;} // abstract Observer abstract class Observer {public abstract function Update () ;}// specific Observer class ConcreteObserver extends Observer {private $ name; private $ observerState; public $ subject; public function _ construct (ConcreteSubject $ _ sub, $ _ name) {$ this-> subject =$ _ sub; $ this-> name =$ _ name ;} public function Update () {$ this-> observerState = $ this-> subject-> subject_state; echo observer. $ this-> name. the new status of is :. $ this-> observerState .;}}
Call the client code:
Header (Content-Type: text/html; charset = utf-8); // --------------------------- observer mode ----------------------- require_once. /Observe. php; // front-end $ _ s = new ConcreteSubject (); // baoyin $ baoyin = new ConcreteObserver ($ _ s, Zhang San); $ jiangchao = new ConcreteObserver ($ _ s, li Si); // write down baoyin jiangchao $ _ s-> Attach ($ baoyin); $ _ s-> Attach ($ jiangchao ); // The front-end finds the boss is back $ _ s-> subject_state = Sun Zong is back; // The front-end sends a notification $ _ s-> Notify ();
Advantages:
1. an abstract model has two aspects, one of which depends on the other. Encapsulate these aspects in independent objects so that they can be changed and reused independently.
2. changes to an object will also lead to changes to one or more objects, without knowing how many objects will change, which can reduce the coupling between objects.
3. an object must notify other objects without knowing who these objects are. You need to create A trigger chain in the system. the behavior of object A will affect object B, and the behavior of object B will affect object C ......, You can create a chain trigger mechanism in observer mode.
Observer Mode (sometimes referred to as publish-subscribe mode ). In this mode, a target object manages all observer objects dependent on it ,...