<?php #观察者模式 /** 观察者模式属于行为模式,是定义对象间的一种一对多的依赖关系,以便当一个对象的状态发生改变时,所有依 赖于它的对象都得到通知并自动刷新。 它完美的将观察者对象和被观察者对象分离。 可以在独立的对象(主体)中维护一个对主体感兴趣的依赖项(观察器)列表。 让所有观察器各自实现公共的 Observer 接口,以取消主体和依赖性对象之间的直接依赖关系。 */ //具体主题角色 class user implements SplSubject{ public $lognum; public $hobby; protected $observers = array(); public function __construct($hobby){ $this->lognum = rand(1,10); $this->hobby = $hobby; // $this->observers = new SplObjectStorage(); $this->observers = array(); } public function login(){ $this->notify(); } //增加一个新的观察者对象 public function attach(SplObserver $observer){ // $this->observers->attach($observer); array_push($this->observers,$observer); } //除一个已注册过的观察者对象 public function detach(SplObserver $observer){ // $this->observers->detach($observer); $index = array_search($observer, $this->observers); if ($index === FALSE || ! array_key_exists($index, $this->observers)) { return FALSE; } unset($this->observers[$index]); return TRUE; } //通知所有注册过的观察者对象 public function notify(){ // $this->observers->rewind(); // while($this->observers->valid()){ // $observer = $this->observers->current(); // $observer->update($this); // $this->observers->next(); // } if (!is_array($this->observers)) { return FALSE; } foreach ($this->observers as $observer) { $observer->update($this); } return TRUE; } } //观察者角色 class secrity implements SplObserver{ public function update(SplSubject $subject){ if($subject->lognum <5){ echo $subject->lognum." < 5"; }else{ echo $subject->lognum." >= 5"; } } } //观察者角色 class ad implements SplObserver{ public function update(SplSubject $subject){ if($subject->hobby == "sport"){ echo $subject->hobby."我爱运动"; }else{ echo "不爱运动哦!"; } } } $user = new user("sport1"); $user->attach(new secrity()); $user->attach(new ad()); $user->login();
<?php /** * Class Subject * 抽象目标类 */ abstract class Subject { protected $stateNow; protected $observers = []; public function attach(Observer $observer) { array_push($this->observers, $observer); } public function detach(Observer $ob) { $pos = 0; foreach ($this->observers as $viewer) { if ($viewer == $ob) { array_splice($this->observers, $pos, 1); } $pos++; } } public function notify() { foreach ($this->observers as $viewer) { $viewer->update($this); } } } /** * Class ConcreteSubject * 具体目标类 */ class ConcreteSubject extends Subject { public function setState($state) { $this->stateNow = $state; $this->notify(); } public function getState() { return $this->stateNow; } } //抽象观察者 abstract class Observer { abstract public function update(Subject $subject); } //具体观察者 class ConcreteObserverDT extends Observer { private $currentState; public function update(Subject $subject) { $this->currentState = $subject->getState(); echo '<div style="font-size:10px;">'. $this->currentState .'</div>'; } } class ConcreteObserverPhone extends Observer { private $currentState; public function update(Subject $subject) { $this->currentState = $subject->getState(); echo '<div style="font-size:20px;">'. $this->currentState .'</div>'; } } //使用观察者模式 class Client { public function __construct() { $sub = new ConcreteSubject(); $obDT = new ConcreteObserverDT(); $obPhone = new ConcreteObserverPhone(); $sub->attach($obDT); $sub->attach($obPhone); $sub->setState('Hello World'); } } $worker = new Client();
本文来自博客园,作者:孙龙-程序员,转载请注明原文链接:https://www.cnblogs.com/sunlong88/p/8691469.html