Chinaunix首页 | 论坛 | 博客
  • 博客访问: 48617
  • 博文数量: 14
  • 博客积分: 297
  • 博客等级: 二等列兵
  • 技术积分: 547
  • 用 户 组: 普通用户
  • 注册时间: 2012-05-08 21:05
个人简介

努力做自己不喜欢的事情!

文章分类
文章存档

2013年(4)

2012年(10)

我的朋友

分类: C/C++

2012-07-25 20:01:55

观察者模式
 
出版者+订阅者=观察者模式
 
定义:
    观察者模式定义了对象之间的一对多依赖,这样一来,当一个对象(主题)改变状态时,它的所有依赖者(观察者)都会收到通知并自动更新。
 
首先观察者对象要向主题对象注册,告诉主题对象它是一个观察者(即,它对主题对象感兴趣)。这样,当主题对象发生变化时,就会通知观察者。
 
如图:
主题与观察者是一对多关系
 
看一个真实的例子,模拟天气预报系统,当天气数据值(主题)温度、湿度和压强发上变化时,要通知Observer对象,再显示到DisplayElement中。
 
ClassDiagram:
    在这里,天气数据(WeatherData)的变化,由它的一个方法setMeasurements()来模拟实现的,每次set不同的温度(temperature)、湿度(humidity)和压强(pressure),这样来通知Observer对象,并显示在DisplayElement中的display上。
 
Subject.h
  1. class Subject
  2. {
  3. public:
  4.     virtual void registerObserver(Observer* o) = 0;
  5.     virtual void removeObserver(Observer* o) = 0;
  6.     virtual void notifyObserver() = 0;
  7. };

  8. #endif //_SUBJECT_H
WeatherData.h
  1. #if !defined(_WEATHERDATA_H)
  2. #define _WEATHERDATA_H

  3. #include "WeatherData.h"
  4. #include "Subject.h"
  5. #include <string>
  6. #include <vector>

  7. class WeatherData : public Subject
  8. {
  9. public:
  10.     void registerObserver(Observer* o);
  11.     void removeObserver(Observer* o);
  12.     void notifyObserver();

  13.     void setMeasurements(int tem,int hum,int pres);
  14. private:
  15.     int temperature;
  16.     int humidity;
  17.     int pressure;

  18.     std::vector<Observer*> vec_observers;

  19. };

  20. #endif //_WEATHERDATA_H
Observer.h
  1. #if !defined(_OBSERVER_H)
  2. #define _OBSERVER_H


  3. class Observer
  4. {
  5. public:
  6.     virtual void update(int tem,int hum,int pres) = 0;
  7. };

  8. #endif //_OBSERVER_H

CurrentConditionDisplay.h
  1. #if !defined(_CURRENTCONDITIONSDISPLY_H)
  2. #define _CURRENTCONDITIONSDISPLY_H

  3. #include "Observer.h"
  4. #include "DisplayElement.h"
  5. #include "Subject.h"
  6. class CurrentConditionsDisply : public Observer, public DisplayElement
  7. {
  8. public:
  9.     CurrentConditionsDisply(Subject* sub);
  10.     ~CurrentConditionsDisply();
  11.     void update(int tem,int hum,int pres);
  12.     void display();

  13. private:
  14.     int temperature;
  15.     int humidity;
  16.     int pressure;
  17.     Subject* sub;
  18. };

  19. #endif //_CURRENTCONDITIONSDISPLY_H
DisplayElement.h
  1. #if !defined(_DISPLAYELEMENT_H)
  2. #define _DISPLAYELEMENT_H


  3. class DisplayElement
  4. {
  5. public:
  6.     virtual void display() = 0;
  7. };

  8. #endif //_DISPLAYELEMENT_H
Test.cpp

  1. #include "stdafx.h"
  2. #include "WeatherData.h"
  3. #include "CurrentConditionsDisply.h"
  4. #include "ForecastDisplay.h"
  5. #include "StatisticsDisplay.h"
  6. #include "ThirdPartyDisplay.h"
  7. int main(int argc, char* argv[])
  8. {
  9.     printf("Hello World!\n");
  10.     
  11.     WeatherData* sub1 = new WeatherData();
  12.     Observer* obr1 = new CurrentConditionsDisply(sub1);
  13.     Observer* obr2 = new ForecastDisplay(sub1);
  14.     Observer* obr3 = new StatisticsDisplay(sub1);
  15.     Observer* obr4 = new ThirdPartyDisplay(sub1);
  16.     sub1->setMeasurements(100,300,900);
  17.     printf("\n-----------------------------------------------------------------------------\n");
  18.     sub1->setMeasurements(200,400,800);
  19.     //unregister ForecastDisplay
  20.     sub1->removeObserver(obr2);
  21.     printf("\n------------------------------------------------------------------------------\n");
  22.     sub1->setMeasurements(2000,4000,8000);


  23.     return 0;
  24. }


OutPut:

  1. Hello World!
  2. The Current Condition Display: temperature = 100;humidity = 300;pressure = 900
  3. The Forecast Display: temperature = 101;humidity = 301;pressure = 901
  4. The Static Condition Display: temperature = 1000;humidity = 3000;pressure = 9000
  5. The Third Party Condition Display: temperature = 10;humidity = 30;pressure = 90
  6. -----------------------------------------------------------------------------
  7. The Current Condition Display: temperature = 200;humidity = 400;pressure = 800
  8. The Forecast Display: temperature = 201;humidity = 401;pressure = 801
  9. The Static Condition Display: temperature = 2000;humidity = 4000;pressure = 8000
  10. The Third Party Condition Display: temperature = 20;humidity = 40;pressure = 80
  11. ------------------------------------------------------------------------------
  12. The Current Condition Display: temperature = 2000;humidity = 4000;pressure = 8000
  13. The Static Condition Display: temperature = 20000;humidity = 40000;pressure = 80000
  14. The Third Party Condition Display: temperature = 200;humidity = 400;pressure = 800
  15. Press any key to continue
完整的项目代码,VC6.0工程。
 
 
 
阅读(1050) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~