header("Content-type:text/html;charset=utf-8");
/**
/**
- 类名:Order
- 说明:订单类
-
观察者模式的使用
*/
class Order {
/*
- 创建订单
*/
public function create($type,$observer) {
echo '购买成功<br/>';
$observer->runObserver($type,$this);
}
}
/*
- 类名:Email
- 说明:邮件类
*/
class Email {
public static function update($order) {
echo '发送邮件<br/>';
}
}
class Status {
public static function update($order) {
echo '改变订单状态<br/>';
}
}
$type1 = 'buy';
$observer = new Observer();
$observer->addObServer($type1, 'Email');
$observer->addObServer($type1, 'Status');
$order = new Order();
$order->create($type1,$observer);
class Supply{
public function release($type,$observer){
echo "发布成功";
$observer->runObserver($type,$this);
}
}
class Sms{
public static function update($supply){
echo "推送消息";
}
}
$type2 = 'release';
$observer2 = new Observer();
$observer2->addObserver($type2,'Sms');
$supply = new Supply();
$supply->release($type2,$observer2);