Statement: This series of blog reference "Big Talk design mode", author Geoscience.
Observer Pattern (sometimes called publishing - subscription mode). 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 an event-handling system.
UML Class Diagrams:
Important roles:
Abstract notifier Role ( Inotifier ): defines the interface rules for notifications.
Specific notifier role ( Boss ): an interface that implements an abstract notifier, which is notified immediately to the observer by a state change .
Abstract Observer Role ( IObserver ): defines the actions that are made after the notification is received ( Update ) interface rules.
specific observer role ( Jingdong ): implement the specific operation method.
Core code:
Abstract class subject{Private $observers = Array (); Public Function Attach (Observer $observer) {Array_push ($this->observers, $observer); The Public Function Detach (Observer $observer) {foreach ($this->observers as $k = + $v) { if ($v = = $observer) {unset ($this->observers[$k]); }}} function Notify () {foreach ($this->observers as $v) {$v->update (); }}}//specific Notifier (Boss and Secretary) class ConcreteSubject extends subject{public $subject _state;} Abstract Observer 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 state is: ". $this->observerstate." <br/> "; }}
Calling client code:
Header ("Content-type:text/html;charset=utf-8");//-------------------------Observer mode-----------------------require_ Once "./observe/observe.php";//foreground $_s = new ConcreteSubject ();//Huibao $baoyin = new Concreteobserver ($_s, "Zhang San"); $jiangchao = New Concreteobserver ($_s, "John Doe");//Front desk Note Huibao Jiang $_s->attach ($baoyin); $_s->attach ($jiangchao);//front desk found the boss back $_s-> Subject_state = "Sun always comes Back";//front desk send notification $_s->notify ();
Advantages:
1 , an abstract model has two aspects, one of which relies on another. These aspects are encapsulated in separate objects so that they can be individually changed and reused.
2 , a change to one object causes one or more other objects to change. , without knowing how many objects will change, you can reduce the coupling between the objects.
3 , an object must notify other objects without knowing who the objects are. A trigger chain needs to be created in the system, The behavior of the A object will affect the B object, and the behavior of the B object will affect C Object ... , you can use the observer pattern to create a chain-triggered mechanism.
Welcome to follow my video course, the address is as follows, thank you.
PHP Object-oriented design pattern
PHP design Pattern-Observer mode