# # # #观察者模式 * Designed to let an object track a state, know how the state changes, once the state changes, all subscribed objects can be notified # # #使用SPL实现观察者模式 * Splsubject interface has three abstract methods attach (), Detach (), notify () These methods do not return any results * Splobserver interface has a method update () it will get the latest state of the subject state and hand it to the Observer instance * Splobjectstorage Class is described as a mapping from an object to a set of data or objects
Observers = new Splobjectstorage (); }//Add observer public Function attach (\splobserver $observer) {$this->observers->attach ($observer); }//Delete Observer public function detach (\splobserver $observer) {$this->observers->detach ($observer); }//Notification forward public function notify () {foreach ($this->observers as $observer) {$observer->update ($this); }}//Set data public function SetData ($dataNow) {$this->data = $dataNow; } public Function GetData () {return $this->data; }}//SPL Specific Viewer class Concreteobserver implements splobserver{Public Function update (Splsubject $subject) {echo $ Subject->getdata (); }}class client{Public Function __construct () {echo "Create three new concrete observers "; Create observer $ob 1 = new Concreteobserver (); $ob 2 = new Concreteobserver (); $ob 3 = new Concreteobserver (); $subject = new ConcreteSubject (); $subject->setobservers (); $subject->setdata ("Here is U data".)
"); $subject->attach ($ob 1); $subject->attach ($ob 2); $subject->attach ($ob 3); $subject->notify (); echo "
Reset Data and Reattach OB3
"; $subject->setdata ("More data, only".)
"); $subject->attach ($ob 3); $subject->attach ($ob 2); $subject->notify (); }} $data = new Client (); "' # # #自由的PHP和观察者模式"
Observers, $obser); The Public Function Detachobser (Observer $obser) {//Removes data from the array $position = 0; foreach ($this->observers as $viwer) {+ + $position; if ($viwer = = $obser) {array_splice ($this->observers, ($position), 1); }}} public function notify () {foreach ($this->observers as $viewer) {//Call the Update method $viewer->update ($this); }}}class ConcreteSubject extends subject{public function setState ($stateSet) {$this->statenow = $stateSe T } public Function GetState () {return $this->statenow; }}class Concreteobserverdt implements observer{private $currentState; Public Function Update (Subject $subject) {$this->currentstate = $subject->getstate (); echo "DT"; }}class Concreobservertablet implements observer{private $currentState; Public Function Update (Subject $subject) {$this->curreNtstate = $subject->getstate (); echo "tablet"; }}class Concreobserverphone implements observer{private $currentState; Public Function Update (Subject $subject) {$this->currentstate = $subject->getstate (); echo "Phone"; }}class client{Public Function __construct () {$sub = new ConcreteSubject (); $ob 1 = new Concreobserverphone (); $ob 2 = new Concreobservertablet (); $ob 3 = new Concreteobserverdt (); $sub->attachobser ($ob 1); $sub->attachobser ($ob 2); $sub->attachobser ($ob 3); $sub->setstate ("fuck"); $sub->detachobser ($ob 1); $sub->notify (); }} $data = new Client ();