本文实例讲述了PHP实现的观察者模式。分享给大家供大家参考,具体如下:
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();
'; } } 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();
更多关于PHP相关内容感兴趣的读者可查看本站专题:《》、《》、《》、《》、《@L_403_2@》及《PHP常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。
原文链接:https://www.f2er.com/php/17245.html