观察者模式
来源:互联网 发布:白鹤祥java架构师视频 编辑:程序博客网 时间:2024/06/05 23:06
1 <?php 2 //定义观察者调用接口 3 class transfer{ 4 protected $_observers = array(); 5 6 //注册对象 7 public function register($sub){ 8 $this->_observers[] = $sub; 9 }10 11 //外部统一调用12 public function trigger(){13 if(!empty($this->_observers)){14 foreach($this->_observers as $observer){15 $observer->update();16 } 17 }18 }19 }20 21 //观察者接口22 interface obserable{23 public function update();24 }25 26 //实现观察者27 class listen implements obserable{28 public function update(){29 echo 'now first time you need to do listen<br/>';30 }31 }32 33 class read implements obserable{34 public function update(){35 echo 'now first time you need to read<br/>';36 }37 }38 39 class speak implements obserable{40 public function update(){41 echo 'now first time you need to speak<br/>';42 }43 }44 45 class write implements obserable{46 public function update(){47 echo 'now first time you need to write<br/>';48 }49 }50 51 $transfer = new transfer();52 $transfer->register(new listen());53 $transfer->register(new read());54 $transfer->register(new speak());55 $transfer->register(new write());56 $transfer->trigger();
0 0
- 观察者模式
- 观察者模式
- 观察者模式
- 观察者模式
- 观察者模式
- 观察者模式
- 观察者模式
- 观察者模式
- 观察者模式
- 观察者模式
- 观察者模式
- 观察者模式
- 观察者模式
- 观察者模式
- 观察者模式
- 观察者模式
- 观察者模式
- 观察者模式
- HDU 1008 Elevator ACM基础题 关键是读懂题目就OK啦
- TCP/IP四层模型和OSI七层模型的概念
- session写入memcache
- Java数据类型
- poj 1151 & hdu 1542 Atlantis(线段树,扫描线)
- 观察者模式
- 字节流与字符流的详解,文件流,数据流
- [机器学习]矩阵的奇异值与特征值有什么相似之处与区别之处?
- 好用的C++API直连MySql方法
- 适配器模式
- iOS10--消息通知的基本使用
- java中的socekt和WebSocekt
- Windows下Nginx的启动、停止等命令
- 正确的注销PHP SESSION