<?php
/*观察者模式:当一个对象状态发生改变时,通知依赖他的全部对象都发生改变*/
abstract class EventGenerator
{
private $observers = array();
public function addObserver(Observer $observer)
{
$this->observers[] = $observer;
}
public function notice($eventinfo = null)
{
foreach ($this->observers as $observer) {
$observer->update();
}
}
}
interface Observer
{
public function update($eventinfo = null);
}
class Observer1 implements Observer
{
public function update($eventinfo = null)
{
echo "1";
}
}
class Event extends EventGenerator
{
public function trigger()
{
$this->notice();
}
}
$event = new Event();
$event->addObserver(new observer1);
$event->trigger();
?>
/*观察者模式:当一个对象状态发生改变时,通知依赖他的全部对象都发生改变*/
abstract class EventGenerator
{
private $observers = array();
public function addObserver(Observer $observer)
{
$this->observers[] = $observer;
}
public function notice($eventinfo = null)
{
foreach ($this->observers as $observer) {
$observer->update();
}
}
}
interface Observer
{
public function update($eventinfo = null);
}
class Observer1 implements Observer
{
public function update($eventinfo = null)
{
echo "1";
}
}
class Event extends EventGenerator
{
public function trigger()
{
$this->notice();
}
}
$event = new Event();
$event->addObserver(new observer1);
$event->trigger();
?>