设计原则:
<?php class Paper{ /* 主题 */ private $_observers = array(); public function register($sub){ /* 注册观察者 */ $this->_observers[] = $sub; } public function trigger(){ /* 外部统一访问 */ if(!empty($this->_observers)){ foreach($this->_observers as $observer){ $observer->update(); } } } } /** * 观察者要实现的接口 */ interface Observerable{ public function update(); } class Subscriber implements Observerable{ public function update(){ echo "Callback\n"; } } 下面是测试代码 /* 测试 */ $paper = new Paper(); $paper->register(new Subscriber()); //$paper->register(new Subscriber1()); //$paper->register(new Subscriber2()); $paper->trigger();
这个是比较浅显易懂的。PHP的SPL(standard php library)提供了两个接口来实现观察者模式;
The SplSubject interface(http://php.net/manual/en/class.splsubject.php)
The SplObserver interface(http://php.net/manual/en/class.splobserver.php)
手册上的例子:
<?php /** * Subject,that who makes news */ class Newspaper implements \SplSubject{ private $name; private $observers = array(); private $content; public function __construct($name) { $this->name = $name; } //add observer public function attach(\SplObserver $observer) { $this->observers[] = $observer; } //remove observer public function detach(\SplObserver $observer) { $key = array_search($observer,$this->observers, true); if($key){ unset($this->observers[$key]); } } //set breakouts news public function breakOutNews($content) { $this->content = $content; $this->notify(); } public function getContent() { return $this->content." ({$this->name})"; } //notify observers(or some of them) public function notify() { foreach ($this->observers as $value) { $value->update($this); } } } /** * Observer,that who recieves news */ class Reader implements SplObserver{ private $name; public function __construct($name) { $this->name = $name; } public function update(\SplSubject $subject) { echo $this->name.‘ is reading breakout news <b>‘.$subject->getContent().‘</b><br>‘; } } $newspaper = new Newspaper(‘Newyork Times‘); $allen = new Reader(‘Allen‘); $jim = new Reader(‘Jim‘); $linda = new Reader(‘Linda‘); //add reader $newspaper->attach($allen); $newspaper->attach($jim); $newspaper->attach($linda); //remove reader $newspaper->detach($linda); //set break outs $newspaper->breakOutNews(‘USA break down!‘); //=====output====== //Allen is reading breakout news USA break down! (Newyork Times) //Jim is reading breakout news USA break down! (Newyork Times)
当新对象要填入的时候,只需要在主题(又叫可观察者)中进行注册(注册方式很多,你也可以在构造的时候,或者框架访问的接口中进行注册),然后实现代码直接在新对象的接口中进行。这降低了主题对象和观察者对象的耦合度。
原文:http://www.cnblogs.com/mingaixin/p/4335700.html