yuchao 发表于 2018-2-11 11:52:55


            本文实例讲述了PHP实现的观察者模式。分享给大家供大家参考,具体如下:
_observers[] = $sub;
    }
    //外部统一调用
    public function trigger(){
      if(!empty($this->_observers)){
      foreach($this->_observers as $observer){
          $observer->update();
      }
      }
    }
}
//观察者接口
interface obserable{
    public function update();
}
//实现观察者
class listen implements obserable{
    public function update(){
      echo 'now first time you need to do listen
';
    }
}
class read implements obserable{
    public function update(){
      echo 'now first time you need to read
';
    }
}
class speak implements obserable{
    public function update(){
      echo 'now first time you need to speak
';
    }
}
class write implements obserable{
    public function update(){
      echo 'now first time you need to write
';
    }
}
$transfer = new transfer();
$transfer->register(new listen());
$transfer->register(new read());
$transfer->register(new speak());
$transfer->register(new write());
$transfer->trigger();
页: [1]
查看完整版本: PHP实现的观察者模式实例