设计模式-观察者模式
来源:互联网 发布:面包机哪个牌子好 知乎 编辑:程序博客网 时间:2024/04/30 16:03
<?php
/*
*观察者模式:观察者设计模式能够更便利的创建和查看目标对象状态的对象,并且提供与核心对象非耦合的指定功能性
*/
class CD{
public $title = '';
public $band = "";
protected $_observers = array();
public function __construct( $title, $band) {
$this->title = $title;
$this->band = $band;
}
public function attachObserver( $type, $observer){
$this->_observers[$type][] = $observer;
}
public function notifyObserver($type){
if(isset ($this->_observers[$type])){
foreach($this->_observers[$type] as $observer){
$observer->update($this);
}
}
}
public function buy(){
$this->notifyObserver("purchased");
}
}
class buyCDNotifyStreamObserver{
public function update(CD $cd){
$activity = "The CD namde {$cd->title} by";
$activity.="{$cd->band} was just purchased";
activityStream::addNewItem( $activity);
}
}
class activityStream{
public static function addNewItem($item){
print $item;
}
}
$title = 'waste of a rib';
$band = 'never again';
$cd = new CD($tiltle, $band);
$observer = new buyCDNotifyStreamObserver();
$cd->attachObserver($type, $observer);
$cd->buy();
?>
/*
*观察者模式:观察者设计模式能够更便利的创建和查看目标对象状态的对象,并且提供与核心对象非耦合的指定功能性
*/
class CD{
public $title = '';
public $band = "";
protected $_observers = array();
public function __construct( $title, $band) {
$this->title = $title;
$this->band = $band;
}
public function attachObserver( $type, $observer){
$this->_observers[$type][] = $observer;
}
public function notifyObserver($type){
if(isset ($this->_observers[$type])){
foreach($this->_observers[$type] as $observer){
$observer->update($this);
}
}
}
public function buy(){
$this->notifyObserver("purchased");
}
}
class buyCDNotifyStreamObserver{
public function update(CD $cd){
$activity = "The CD namde {$cd->title} by";
$activity.="{$cd->band} was just purchased";
activityStream::addNewItem( $activity);
}
}
class activityStream{
public static function addNewItem($item){
print $item;
}
}
$title = 'waste of a rib';
$band = 'never again';
$cd = new CD($tiltle, $band);
$observer = new buyCDNotifyStreamObserver();
$cd->attachObserver($type, $observer);
$cd->buy();
?>
- 模式设计:观察者模式
- 设计模式-----观察者模式
- 设计模式-观察者模式
- 设计模式--观察者模式
- 设计模式:观察者模式
- 设计模式-----观察者模式
- 设计模式:观察者模式
- 设计模式-观察者模式
- 设计模式 观察者模式
- 设计模式-观察者模式
- 设计模式-【观察者模式】
- 设计模式-观察者模式
- 设计模式 -- 观察者模式
- 设计模式-观察者模式
- 【设计模式】观察者模式
- 设计模式- 观察者模式
- 设计模式- 观察者模式
- 设计模式--观察者模式
- 【模拟】时间计算
- SAP LSMW批处理使用方法(5)_步骤3
- 敏捷开发之 12条敏捷原则
- 北京公司真金白银造月饼 被用来向政府机关送礼
- 实践考核(二)
- 设计模式-观察者模式
- 大二女生向铁道部申请公开1.99亿客票系统信息
- 韩称将强化对中国专属区内苏岩礁“警备”
- 【动态规划】最优时间表
- SAP LSMW批处理使用方法(6)_步骤4、5
- [HDU-1240] 三维的FLOODFILL
- ARM汇编编程基础(二) -- 基本寻址方式与基本指令
- Servlet-----filter 过滤器
- 摄像机标定原理 源代码及解析(基于VC6.0+OpenC1.0)