标签:str array 它的 编程方式 this mil add abstract upd
定义:当一个对象状态发生改变时,依赖它的对象全部会收到通知,并自动更新,实现低耦合,非侵入式的通知与更新机制$event = new Event();
$event->addObserver(new Observer1());
$event->addObserver(new Observer2());
$event->trigger();
class Event extends \IMooc\EventGenerator
{
function trigger()
{
echo "Event<br/>\n";
$this->notify();
}
}class Observer1 implements \IMooc\Observer
{
function update($event_info = null)
{
echo "逻辑1<br/>\n";
}
}
class Observer2 implements \IMooc\Observer
{
function update($event_info = null)
{
echo "逻辑2<br/>\n";
}
}<?php
namespace IMooc;
abstract class EventGenerator
{
private $observers = array();
function addObserver(Observer $observer)
{
$this->observers[] = $observer;
}
function notify()
{
foreach ($this->observers as $observer)
{
$observer->update();
}
}
}<?php
namespace IMooc;
interface Observer
{
function update($event_info = null);
}
标签:str array 它的 编程方式 this mil add abstract upd
原文地址:http://www.cnblogs.com/phonecom/p/c9af18ff116c167489f08c149cefd9e7.html