1、观察者模式:定义了对象之间的一对多依赖,这样一来,当一个对象改变状态时,它的所有依赖者都会收到通知并且有所作为。即出版者+订阅者=观察者模式。
2、例子类图概览:
3、假设我们需要实现一个气象台,当气象信息更新时候,通知到每一个布告板上。
(1)我们根据上图将所需接口列出如下:
(2)然后我们实现主题接口如下:
arrObserver[] = $O; } /** * 移除观察对象 * @param Observer $O */ public function removeObserver(Observer $O) { foreach ($this->arrObserver as $key => $o){ if($o == $O){ unset($this->arrObserver[$key]); } } } /** * 通知观察对象更新数据 */ public function notifyObservers() { foreach ($this->arrObserver as $key => $o){ $o->update($this->fltTemperature, $this->fltHumidity, $this->fltPressure); } } /** * 当信息更新时候调用通知接口 */ public function measurementsChange(){ $this->notifyObservers(); } /** * 更新信息 * @param $fltTemperature * @param $fltHumidity * @param $fltPressure */ public function setMeasurements($fltTemperature, $fltHumidity, $fltPressure){ $this->fltTemperature = $fltTemperature; $this->fltHumidity = $fltHumidity; $this->fltPressure = $fltPressure; $this->measurementsChange(); }}
(3)我们再实现观察者接口如下:
fltTemperature = $fltTemperature; $this->fltHumidity = $fltHumidity; $this->fltPressure = $fltPressure; $this->display(); } public function display() { echo "now condition is: temperatur={ $this->fltTemperature}, humidity={ $this->fltHumidity}, pressure={ $this->fltPressure}"; }}
fltTemperature = $fltTemperature; $this->fltHumidity = $fltHumidity; $this->fltPressure = $fltPressure; $this->display(); } public function display() { if($this->fltTemperature > 30 && $this->fltHumidity > 70 && $this->fltPressure > 80){ echo 'it will be hot'; }else{ echo 'it will be cool'; } }}
(4)最后我们对相关接口进行调用输出:
setMeasurements(40,20,50.9);$objWeather->setMeasurements(20,40,30.7);$objWeather->setMeasurements(10,50,60.2);