观察者模式
出版者+订阅者=观察者模式
定义:
观察者模式定义了对象之间的一对多依赖,这样一来,当一个对象(主题)改变状态时,它的所有依赖者(观察者)都会收到通知并自动更新。
首先观察者对象要向主题对象注册,告诉主题对象它是一个观察者(即,它对主题对象感兴趣)。这样,当主题对象发生变化时,就会通知观察者。
如图:
主题与观察者是一对多关系
看一个真实的例子,模拟天气预报系统,当天气数据值(主题)温度、湿度和压强发上变化时,要通知Observer对象,再显示到DisplayElement中。
ClassDiagram:
在这里,天气数据(WeatherData)的变化,由它的一个方法setMeasurements()来模拟实现的,每次set不同的温度(temperature)、湿度(humidity)和压强(pressure),这样来通知Observer对象,并显示在DisplayElement中的display上。
Subject.h
- class Subject
- {
- public:
- virtual void registerObserver(Observer* o) = 0;
- virtual void removeObserver(Observer* o) = 0;
- virtual void notifyObserver() = 0;
- };
- #endif //_SUBJECT_H
WeatherData.h
- #if !defined(_WEATHERDATA_H)
- #define _WEATHERDATA_H
- #include "WeatherData.h"
- #include "Subject.h"
- #include <string>
- #include <vector>
- class WeatherData : public Subject
- {
- public:
- void registerObserver(Observer* o);
- void removeObserver(Observer* o);
- void notifyObserver();
- void setMeasurements(int tem,int hum,int pres);
- private:
- int temperature;
- int humidity;
- int pressure;
- std::vector<Observer*> vec_observers;
- };
- #endif //_WEATHERDATA_H
Observer.h
- #if !defined(_OBSERVER_H)
- #define _OBSERVER_H
- class Observer
- {
- public:
- virtual void update(int tem,int hum,int pres) = 0;
- };
- #endif //_OBSERVER_H
CurrentConditionDisplay.h
- #if !defined(_CURRENTCONDITIONSDISPLY_H)
- #define _CURRENTCONDITIONSDISPLY_H
- #include "Observer.h"
- #include "DisplayElement.h"
- #include "Subject.h"
- class CurrentConditionsDisply : public Observer, public DisplayElement
- {
- public:
- CurrentConditionsDisply(Subject* sub);
- ~CurrentConditionsDisply();
- void update(int tem,int hum,int pres);
- void display();
- private:
- int temperature;
- int humidity;
- int pressure;
- Subject* sub;
- };
- #endif //_CURRENTCONDITIONSDISPLY_H
DisplayElement.h
- #if !defined(_DISPLAYELEMENT_H)
- #define _DISPLAYELEMENT_H
- class DisplayElement
- {
- public:
- virtual void display() = 0;
- };
- #endif //_DISPLAYELEMENT_H
Test.cpp
- #include "stdafx.h"
- #include "WeatherData.h"
- #include "CurrentConditionsDisply.h"
- #include "ForecastDisplay.h"
- #include "StatisticsDisplay.h"
- #include "ThirdPartyDisplay.h"
- int main(int argc, char* argv[])
- {
- printf("Hello World!\n");
-
- WeatherData* sub1 = new WeatherData();
- Observer* obr1 = new CurrentConditionsDisply(sub1);
- Observer* obr2 = new ForecastDisplay(sub1);
- Observer* obr3 = new StatisticsDisplay(sub1);
- Observer* obr4 = new ThirdPartyDisplay(sub1);
- sub1->setMeasurements(100,300,900);
- printf("\n-----------------------------------------------------------------------------\n");
- sub1->setMeasurements(200,400,800);
- //unregister ForecastDisplay
- sub1->removeObserver(obr2);
- printf("\n------------------------------------------------------------------------------\n");
- sub1->setMeasurements(2000,4000,8000);
- return 0;
- }
OutPut:
- Hello World!
- The Current Condition Display: temperature = 100;humidity = 300;pressure = 900
- The Forecast Display: temperature = 101;humidity = 301;pressure = 901
- The Static Condition Display: temperature = 1000;humidity = 3000;pressure = 9000
- The Third Party Condition Display: temperature = 10;humidity = 30;pressure = 90
- -----------------------------------------------------------------------------
- The Current Condition Display: temperature = 200;humidity = 400;pressure = 800
- The Forecast Display: temperature = 201;humidity = 401;pressure = 801
- The Static Condition Display: temperature = 2000;humidity = 4000;pressure = 8000
- The Third Party Condition Display: temperature = 20;humidity = 40;pressure = 80
- ------------------------------------------------------------------------------
- The Current Condition Display: temperature = 2000;humidity = 4000;pressure = 8000
- The Static Condition Display: temperature = 20000;humidity = 40000;pressure = 80000
- The Third Party Condition Display: temperature = 200;humidity = 400;pressure = 800
- Press any key to continue
完整的项目代码,VC6.0工程。
阅读(1038) | 评论(0) | 转发(0) |