用PHP是现在观察者模式。个人觉得观察者模式的重点在于观察者接口以及观察者的注册。以下是代码:
1 <?php
2
3 interface Observer {
4 public function update($time);
5 }
6
7 ?>
2
3 interface Observer {
4 public function update($time);
5 }
6
7 ?>
可观察者(发布者)类
<?php
class Observable {
private $_observers = array();
private $_time;
public function registerObserver(Observer $observer) {
$this->_observers[] = $observer;
}
public function removeObserver(Observer $observer) {
if ($index = array_search($observer, $this->_observers)) {
unset($this->_observers[$index]);
}
}
public function notifyObservers() {
foreach ($this->_observers as $observer) {
$observer->update($this->_time);
}
}
private function _measurementsChanged() {
$this->notifyObservers();
}
public function setMeasurements($time) {
$this->_time = $time;
$this->_measurementsChanged();
}
}
?>
class Observable {
private $_observers = array();
private $_time;
public function registerObserver(Observer $observer) {
$this->_observers[] = $observer;
}
public function removeObserver(Observer $observer) {
if ($index = array_search($observer, $this->_observers)) {
unset($this->_observers[$index]);
}
}
public function notifyObservers() {
foreach ($this->_observers as $observer) {
$observer->update($this->_time);
}
}
private function _measurementsChanged() {
$this->notifyObservers();
}
public function setMeasurements($time) {
$this->_time = $time;
$this->_measurementsChanged();
}
}
?>
1 <?php
2 include_once 'Observer.php';
3
4 class ObserverA implements Observer{
5 public function __construct(Observable $observable) {
6 $observable->registerObserver($this);
7 }
8
9 public function update($time) {
10 echo 'ObserverA: received time is '.$time."\r\n";
11 }
12 }
13
14 ?>
2 include_once 'Observer.php';
3
4 class ObserverA implements Observer{
5 public function __construct(Observable $observable) {
6 $observable->registerObserver($this);
7 }
8
9 public function update($time) {
10 echo 'ObserverA: received time is '.$time."\r\n";
11 }
12 }
13
14 ?>
1 <?php
2 include_once 'Observer.php';
3
4 class ObserverB implements Observer{
5 public function __construct(Observable $observable) {
6 $observable->registerObserver($this);
7 }
8
9 public function update($time) {
10 echo 'ObserverB: received time is '.$time."\r\n";
11 }
12 }
13
14 ?>
2 include_once 'Observer.php';
3
4 class ObserverB implements Observer{
5 public function __construct(Observable $observable) {
6 $observable->registerObserver($this);
7 }
8
9 public function update($time) {
10 echo 'ObserverB: received time is '.$time."\r\n";
11 }
12 }
13
14 ?>
1 <?php
2 include 'Observable.php';
3 include 'ObserverA.php';
4 include 'ObserverB.php';
5
6 $observable = new Observable();
7
8 $observerA = new ObserverA($observable);
9 $observerB = new ObserverB($observable);
10
11 $observable->setMeasurements('2010-4-4');
12 $observable->setMeasurements('2010-4-5');
13 $observable->setMeasurements('2010-4-10');
14 ?>
2 include 'Observable.php';
3 include 'ObserverA.php';
4 include 'ObserverB.php';
5
6 $observable = new Observable();
7
8 $observerA = new ObserverA($observable);
9 $observerB = new ObserverB($observable);
10
11 $observable->setMeasurements('2010-4-4');
12 $observable->setMeasurements('2010-4-5');
13 $observable->setMeasurements('2010-4-10');
14 ?>
运行结果: