观察者模式
在此种模式中,一个目标物件管理所有相依于它的观察者物件,并且在它本身的状态改变时主动发出通知。这通常透过呼叫各观察者所提供的方法来实现。此种模式通常被用来实现事件处理系统。
Demo:
//抽象通知者
abstract class Subject
{
private $observers = array();
public function Attach(Observer $observer)
{
array_push($this->observers,$observer);
}
public function Detach(Observer $observer)
{
foreach($this->observers as $k=>$v)
{
if($v==$observer)
{
unset($this->observers[$k]);
}
}
}
function Notify()
{
foreach($this->observers as $v)
{
$v->Update();
}
}
}
//具体通知者(Boss和Secretary)
class ConcreteSubject extends Subject
{
public $subject_state;
}
//抽象观察者
abstract class Observer
{
public abstract function Update();
}
//具体观察者
class ConcreteObserver extends Observer
{
private $name;
private $observerState;
public $subject;
public function __construct(ConcreteSubject $_sub,$_name)
{
$this->subject = $_sub;
$this->name = $_name;
}
public function Update()
{
$this->observerState = $this->subject->subject_state;
echo "观察者".$this->name."的新状态是:".$this->observerState."<br/>";
}
}
//-------------------------观察者模式-----------------------
require_once "./Observe/Observe.php";
//前台
$_s = new ConcreteSubject();
//宝银
$baoyin = new ConcreteObserver($_s, "张三");
$jiangchao = new ConcreteObserver($_s,"李四");
//前台记下宝银姜超
$_s->Attach($baoyin);
$_s->Attach($jiangchao);
//前台发现老板回来
$_s->subject_state = "孙总回来了";
//前台发送通知
$_s->Notify();