This article mainly introduces the observer mode in the php design mode and uses php to implement the observer mode. if you are interested, refer to it.
I. intention
Define a one-to-many dependency between objects. when the status of an object changes, all objects dependent on it are notified and automatically updated. [GOF95]
It is also known as the Publish-Subscribe mode, Model-View mode, Source-Listener mode, or Dependents mode.
II. Observer pattern structure
III. main roles in Observer Mode
Abstract topic role:The topic role saves all references to the Observer object in a collection. each topic can have any number of observers. Abstract topics provide interfaces for adding and deleting observer objects.
Abstract Observer role:Define an interface for all the specific observers and update themselves when the observed theme changes.
The specific topic (ConcreteSubject) role:Stores the relevant status to a specific observer object. when the internal status of a specific topic changes, a notification is sent to all registered observers. A topic role is usually implemented by a specific subclass.
The specific observer (ConcretedObserver) role:Stores a specific topic object, stores the relevant status, and implements the update interface required by the abstract observer role, so that the state of the object is consistent with that of the topic.
IV. Advantages and disadvantages of observer mode
Observer ModeAdvantages:
1. the coupling between the observer and the topic is small;
2. supports broadcast communication;
Observer ModeDisadvantages:
1. because the observer does not know the existence of other observers, it may have no idea about the final cost of changing the target. This may cause unexpected updates.
5. observer mode applicable scenarios
1. When an abstract model has two aspects, one of which depends on the other.
2. when changing an object, you need to change other objects at the same time without knowing how many objects are to be changed.
3. When an object must notify other objects, it cannot assume who the other objects are. In other words, you do not want these objects to be tightly coupled.
VI. observer mode and other modes
Mediator ):By encapsulating complex update semantics, ChangeManager acts as the intermediary between the target and the Observer.
Singleton mode ):ChangeManager can use the Singleton mode to ensure that it is unique and globally accessible.
VII. observer mode PHP example
<? Php/*** abstract topic role */interface Subject {/*** adds a new Observer object * @ param observer $ Observer */public function attach (observer $ Observer ); /*** delete a registered Observer object * @ param observer $ Observer */public function detach (observer $ Observer ); /*** notify all registered observer objects */public function notifyObservers ();}/*** specific topic role */class ConcreteSubject implements Subject {private $ _ observers; public function _ construct () {$ thi S-> _ observers = array ();}/*** adds a new Observer object * @ param observer $ Observer */public function attach (observer $ Observer) {return array_push ($ this-> _ observers, $ observer );} /*** delete a registered Observer object * @ param observer $ Observer */public function detach (observer $ observer) {$ index = array_search ($ Observer, $ this-> _ observers); if ($ index = FALSE |! Array_key_exists ($ index, $ this-> _ observers) {return FALSE;} unset ($ this-> _ observers [$ index]); return TRUE ;} /*** notify all registered observer objects */public function policyobservers () {if (! Is_array ($ this-> _ observers) {return FALSE;} foreach ($ this-> _ observers as $ observer) {$ observer-> update ();} return TRUE;}/*** abstract Observer role */interface Observer {/*** update method */public function update ();} class ConcreteObserver implements Observer {/*** Observer name * @ var
*/Private $ _ name; public function _ construct ($ name) {$ this-> _ name = $ name ;} /*** update method */public function update () {echo 'observer', $ this-> _ name, 'has notified.
';}}/*** Client */class Client {/*** Main program. */public static function main () {$ subject = new ConcreteSubject ();/* add the first observer */$ observer1 = new ConcreteObserver ('Martin '); $ subject-> attach ($ observer1); echo'
The First sector Y:
'; $ Subject-> policyobservers ();/* Add a second observer */$ observer2 = new ConcreteObserver ('phppanc'); $ subject-> attach ($ observer2 ); echo'
The Second policy:
'; $ Subject-> policyobservers ();/* delete the first observer */$ subject-> detach ($ observer1); echo'
The Third policy:
'; $ Subject-> policyobservers () ;}} Client: main ();?>
The above is the code that uses php to implement the observer mode. there are some concepts about the observer mode, which is helpful for your learning.