目的
定义对象之间一对多的以来关系,当一个对象改变时所有依赖他的对象都得到通知并自定更新。
abstract class Subject{
protected $observers;
public function attach(Observer $m){
$this->observers[] = $m;
}
public function detach(Observer $m){
unset($this->observers[array_search($m,$this->observers)]);
}
public function notify(){
foreach($this->observers as $item){
$item->Update();
}
}
}
class ConcreteSubject extends Subject{
protected $subjectState;
public function __construct(){
$this->observers = [];
}
public function setState($o){
$this->subjectState = $o;
}
public function getState(){
return $this->subjectState;
}
}
abstract class Observer {
public abstract function update();
}
class ConcreteObserver extends Observer{
private $observerState;
private $subject;
public function __construct($subject){
$this->subject = $subject;
}
public function update(){
$this->observerState = $this->subject->getState();
echo "Subjectstate has modified".", now :".$this->observerState;
}
}
//测试代码
$subject = new ConcreteSubject();
$observers = new ConcreteObserver($subject);
$subject->attach($observers);
$subject->setState('sate1');
$subject->notify();
PREVIOUS设计模式(18)memento模式
NEXT设计模式(20)state模式