Observer Pattern :
Observer Pattern: Defines a one-to-many dependency between objects, so that whenever an object's state changes, its dependent objects are notified and automatically updated. The Observer pattern is also known as the Publish/Subscribe pattern, the Model-View (Model/View) pattern, the Source-Listener (Source/Listener) pattern or the Dependents pattern.
The Observer pattern is an object behavioral pattern.
Pattern Motivation
Establish an object-to-object dependency. When an object changes, it will automatically notify other objects, and other objects will respond accordingly. Here, the changed object is called the observation target, and the notified object is called the observer. An observation target can correspond to multiple observers, and these observers are not connected to each other, and observers can be added and deleted as needed. , making the system easier to extend, which is the pattern motivation of the observer pattern.
The observer pattern includes the following roles:
Subject: target
ConcreteSubject: concrete target
Observer: observer
ConcreteObserver: concrete observer
UML class:
Applicability:
When an abstract model has two aspects, one of which depends on the other. Encapsulate the two in separate objects so that they can be changed and reused independently of each other.
When a change to one 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, and it cannot assume who the other objects are. In other words, you don't want these objects to be tightly coupled
//Code
<?php header("Content-type:text/html;Charset=utf-8"); //target interface, which defines the method to be implemented by the observation target abstract class Subject{ abstract function attach(Observer $observer); //Add observer abstract function detach(Observer $observer); //Remove the observer abstract function notify(); //Notify all observers of modification when conditions are met abstract function condition($num); //Condition to initiate notification } //Specific observation target class ConcreteSubject extends Subject{ private $observers = array(); // add observer function attach(Observer $observer){ $this->observers[] = $observer; } // remove the observer function detach(Observer $observer){ $key=array_search($observer, $this->observers); if($key !== false){ //Be careful not to write !=, the expression 0!= flase is flase unset($this->observers[$key]); } } //Notify all observers of the modification function notify(){ foreach($this->observers as $observer){ $observer->update(); } } //Condition to initiate notification function condition($num){ if($num>100){ $this->notify(); } } } //Abstract observer interface, define properties common to all observers - perform modification abstract class Observer{ abstract function update(); } //Concrete observer class, implements abstract observer interface class ConcreteObserverA extends Observer{ function update(){ echo "Report A: The enemy army has more than 100 people, quickly withdraw!<br>"; } //other functions function eat(){ echo "A is eating"; } } class ConcreteObserverB extends Observer{ function update(){ echo "Report B: The enemy army has more than 100 people, quickly withdraw!<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); //Remove observer A $concreteSubject->attach($observerA); $concreteSubject->attach($observerB); $concreteSubject->condition(1000); ?>