PHP Viewer pattern and PHP implementation Observer pattern

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 of observing the target to be realized abstract class subject{abstract function Attach (Observer $observer);  Add observer abstract function detach (Observer $observer);  Remove Observer abstract function notify (); Notify all observers to modify the abstract function condition ($num) when the condition is met;    The conditions for initiating the notification}//specific observation target class ConcreteSubject extends subject{private $observers = Array ();    Add Observer function Attach (Observer $observer) {$this->observers[] = $observer;         }//Remove Observer function detach (Observer $observer) {$key =array_search ($observer, $this->observers);         if ($key!== false) {//note do not write to! =, the expression 0!=flase is flase unset ($this->observers[$key]); }}//Notify all observers to modify function notify () {foreach ($this->observers as $observer) {$observer->u        Pdate ();        }}//The conditions for initiating the notification function condition ($num) {if ($num >100) {$this->notify (); }}}//an abstract observer interface that defines the properties common to all observers--performing modifications AbstrAct class observer{abstract function Update ();} Specific observer classes, implementing the abstract Viewer interface class Concreteobservera extends observer{function update () {echo "a report: The enemy has more than 100 people, get out!    <br> ";    }//Other function functions eat () {echo "A is eating"; }}class Concreteobserverb extends observer{function update () {echo ' B Report: The enemy has more than 100 people, get out!    <br> ";    }//Other functions function sleep () {echo "B is sleeping"; }}//Test $observera = new Concreteobservera (), $observerB = new Concreteobserverb (), $concreteSubject = new ConcreteSubject (  ); $concreteSubject->attach ($observerA);  Add observer A$concretesubject->detach ($observerA); Removal of the Observer A$concretesubject->attach ($observerA); $concreteSubject->attach ($observerB); $concreteSubject condition (?>);

PHP Viewer pattern and PHP implementation Observer pattern

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.