Php design pattern observer pattern, design pattern observer. Php design pattern Observer Pattern. The core of design pattern observer pattern is to separate customer elements (observers) from a central class (subject. When the subject knows the event php design mode observer mode, the design mode observer
The core of the observer mode is to separate the customer element (observer) from a central class (subject. When the subject knows that an event occurs, the observer must be notified. At the same time, we do not want to hard encode the relationship between the subject and the Observer. To achieve this goal, we can allow the observer to register on the subject.
The observer mode implements a low-coupling, non-intrusive notification and update mechanism.
Scenario: when an event occurs, perform a series of update operations.
EventGenerator.php
Observers [] = $ observer;}/*** notify the observer to perform update operations one by one */function every Y () {foreach ($ this-> observers as $ observer) {$ observer-> update ();}}}
Observer.php
Index. php
Class Event extends \ Baobab \ EventGenerator {/*** triggers a new Event */function trigger () {echo 'event occurred '; $ this-> Policy ();}} /*** Observer 1 */class Observer1 implements \ Baobab \ Observer {function Update ($ event_info = null) {echo 'update operation code 1 ';}}
/**
* Observer 2
*/Class Observer2 implements \ Baobab \ Observer {function Update ($ event_info = null) {echo 'update operation code 2'; }}$ event = new Event (); $ event-> addObserver (new Observer1 (); $ event-> addObserver (new Observer2 (); $ event-> trigger ();
Observer Pattern. The core of observer pattern is to separate customer elements (observers) from a central class (subject. When the subject knows the event...