Chinaunix首页 | 论坛 | 博客
  • 博客访问: 170271
  • 博文数量: 60
  • 博客积分: 15
  • 博客等级: 民兵
  • 技术积分: 638
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-26 10:59
个人简介

喜欢coding,因为那是一件伟大的事情,是将无生命的IC赋予灵魂的过程,让我拥有了和上帝一样的成就感。(w1c2g3@163.com)

文章分类

全部博文(60)

文章存档

2017年(7)

2016年(41)

2015年(1)

2014年(4)

2013年(7)

我的朋友

分类: C/C++

2016-10-30 22:39:22

观察者模式 定义了对象之间的一对多依赖,这样一来,当一个对象改变时,他的所有依赖者都会收到通知并自动更新。




  1. #include <iostream>
  2. #include <string>
  3. #include <vector>

  4. using namespace std;


  5. struct Observer {
  6.     virtual void update() {};
  7. };

  8. struct Observable {
  9. public:
  10.     void addObserver(Observer *observer) {
  11.         list.push_back(observer);
  12.     }
  13.     void deleteObserver(Observer *observer) {
  14.         vector<Observer *>::iterator it;
  15.         
  16.         for(it=list.begin(); it!=list.end(); ++it) {
  17.             if ((*it) == observer) {
  18.                 list.erase(it);
  19.                 break;
  20.             }
  21.         }
  22.     }
  23.     void notifyObserver() {
  24.         vector<Observer *>::iterator it;
  25.         for(it=list.begin(); it!=list.end(); ++it) {
  26.             (*it)->update();
  27.         }
  28.     }
  29. private:
  30.     vector<Observer *> list;
  31. };


  32. struct WeatherData : public Observable {
  33. public:
  34.     int getTemperature() {
  35.         return temperature;
  36.     }
  37.     
  38.     int getHumidity() {
  39.         return humidity;
  40.     }

  41.     int getPressure() {
  42.         return pressure;
  43.     }
  44.     
  45.     void setTemperature(int mtemperature) {
  46.         temperature = mtemperature;
  47.     }

  48.     void setHumidity(int mhumidity) {
  49.         humidity = mhumidity;
  50.     }
  51.     
  52.     void setPressure(int mpressure) {
  53.         pressure = mpressure;
  54.     }
  55.     
  56.     void notifyDisplay() {
  57.         notifyObserver();
  58.     }
  59. private:
  60.     int temperature;
  61.     int humidity;
  62.     int pressure;
  63. };


  64. struct GeneralDisplay : public Observer {
  65. public:
  66.     GeneralDisplay(WeatherData *mweatherdata) {
  67.         weatherdata = mweatherdata;
  68.         weatherdata->addObserver(this);
  69.     }
  70.     
  71.     ~GeneralDisplay() {
  72.         weatherdata->deleteObserver(this);
  73.     }
  74.     
  75.     virtual void update() {
  76.         cout << "GeneralDisplay: " <<
  77.             " Temperature: " << weatherdata->getTemperature() <<
  78.             " Humidity: " << weatherdata->getHumidity() <<
  79.             " Pressure: " << weatherdata->getPressure() << endl;
  80.     }
  81.     
  82.     virtual string get_name() {
  83.         return "GeneralDisplay";
  84.     }
  85.     
  86. private:
  87.     WeatherData *weatherdata;
  88. };

  89. struct StatisticsDisplay : public Observer {
  90. public:
  91.     StatisticsDisplay(WeatherData *mweatherdata) {
  92.         weatherdata = mweatherdata;
  93.         weatherdata->addObserver(this);
  94.     }
  95.     
  96.     ~StatisticsDisplay() {
  97.         weatherdata->deleteObserver(this);
  98.     }
  99.     
  100.     virtual void update() {
  101.         cout << "StatisticsDisplay: " <<
  102.             " Temperature: " << weatherdata->getTemperature() <<
  103.             " Humidity: " << weatherdata->getHumidity() <<
  104.             " Pressure: " << weatherdata->getPressure() << endl;
  105.     }
  106.     
  107. private:
  108.     WeatherData *weatherdata;
  109. };

  110. struct ForecaseDisplay : public Observer {
  111. public:
  112.     ForecaseDisplay(WeatherData *mweatherdata) {
  113.         weatherdata = mweatherdata;
  114.         weatherdata->addObserver(this);
  115.     }
  116.     
  117.     ~ForecaseDisplay() {
  118.         weatherdata->deleteObserver(this);
  119.     }
  120.     
  121.     virtual void update() {
  122.         cout << "ForecaseDisplay: " <<
  123.             " Temperature: " << weatherdata->getTemperature() <<
  124.             " Humidity: " << weatherdata->getHumidity() <<
  125.             " Pressure: " << weatherdata->getPressure() << endl;
  126.     }
  127.     
  128. private:
  129.     WeatherData *weatherdata;
  130. };

  131. int main(int argc, char **argv)
  132. {
  133.     WeatherData *weatherData = new WeatherData();
  134.     GeneralDisplay *generaldisplay = new GeneralDisplay(weatherData);
  135.     StatisticsDisplay *statisticsDisplay = new StatisticsDisplay(weatherData);
  136.     ForecaseDisplay *forecaseDisplay = new ForecaseDisplay(weatherData);
  137.     
  138.     weatherData->setTemperature(1);
  139.     weatherData->setHumidity(2);
  140.     weatherData->setPressure(3);
  141.     weatherData->notifyDisplay();
  142.     
  143.     delete forecaseDisplay;
  144.     delete statisticsDisplay;
  145.     delete generaldisplay;
  146.     delete weatherData;
  147. }

阅读(679) | 评论(0) | 转发(0) |
0

上一篇:策略模式

下一篇:装饰者模式

给主人留下些什么吧!~~