Observer patterns and PHP implementations

Source: Internet
Author: User

Viewer mode:
Observer pattern (Observer pattern): Defines a one-to-many dependency between objects, so that whenever an object state changes, its dependent objects are notified and automatically updated. The observer pattern is also called publish-subscribe (publish/subscribe) mode, model-view (model/view) mode, source-listener (Source/listener) mode, or slave (dependents) mode.
The Observer pattern is an object-behavioral pattern.

Mode motive
Establishes a dependency between an object and an object that automatically notifies other objects when an object changes, and the other objects react accordingly. In this case, the object of the change is called the observation target, and the object being notified is called the Observer, and an observation target can correspond to multiple observers, and these observers are not interconnected, and can add and remove observers as needed, making the system more extensible, which is the pattern motive of the observer pattern.

The Observer pattern contains the following roles:
Subject: Target
ConcreteSubject: Specific objectives
Observer: Viewer
Concreteobserver: Specific observers

UML Class Diagrams:

  

Applicability:
When an abstract model has two facets, one aspect depends on the other. The two are encapsulated in separate objects so that they can be individually changed and reused.
When a change to an object needs to change other objects at the same time, it is not known how many objects need to be changed.
When an object must notify other objects, it cannot assume that other objects are who. In other words, you don't want these objects to be tightly coupled.

Code implementation

<?PHPHeader("content-type:text/html; Charset=utf-8 ");//target interface, define the method to be implemented by the observation targetAbstract classsubject{Abstract functionAttach (Observer$observer);//Add Observer   Abstract functionDetach (Observer$observer);//Remove Viewer   Abstract functionNotify ();//notify all observers to modify when conditions are met   Abstract functionCondition$num);//conditions for initiating a notification}//specific observation targetsclassConcreteSubjectextendssubject{Private $observers=Array(); //Add Observer    functionAttach (Observer$observer){         $this->observers[] =$observer; }    //removing observers    functionDetach (Observer$observer){         $key=Array_search($observer,$this-observers); if($key!==false){//Note Do not write! =, expression 0!=flase to Flase              unset($this->observers[$key]); }    }    //notify all observers to modify    functionNotify () {foreach($this->observers as $observer){            $observer-update (); }    }    //conditions for initiating a notification    functionCondition$num){        if($num>100){            $this-notify (); }    }}//abstract Observer interface, defining attributes that all observers have in common--performing modificationsAbstract classobserver{Abstract functionupdate ();}//specific observer classes, implementing an abstract observer interfaceclassConcreteobserveraextendsobserver{functionUpdate () {Echo"A report: The enemy has more than 100 people, go!" <br> "; }    //Other Functions    functioneat () {Echo"A is eating"; }}classConcreteobserverbextendsobserver{functionUpdate () {Echo"B Report: The enemy has more than 100 people, go!" <br> "; }    //Other Functions    function Sleep(){        Echo"B is sleeping."; }}//Test$observerA=NewConcreteobservera ();$observerB=NewConcreteobserverb ();$concreteSubject=NewConcreteSubject ();$concreteSubject->attach ($observerA);//Add Observer a$concreteSubject->detach ($observerA);//Remove Viewer a$concreteSubject->attach ($observerA);$concreteSubject->attach ($observerB);$concreteSubject->condition (1000); ?>

Observer patterns and PHP implementations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.