灯火互联
管理员
管理员
  • 注册日期2011-07-27
  • 发帖数41778
  • QQ
  • 火币41290枚
  • 粉丝1086
  • 关注100
  • 终身成就奖
  • 最爱沙发
  • 忠实会员
  • 灌水天才奖
  • 贴图大师奖
  • 原创先锋奖
  • 特殊贡献奖
  • 宣传大使奖
  • 优秀斑竹奖
  • 社区明星
阅读:3202回复:0

[C++技术]观察者模式 C++ 实现

楼主#
更多 发布于:2013-06-17 10:54
[cpp]
#include<iostream>  
#include<string>  
#include<list>  
#include<algorithm>  
/*
  气象监控应用问题 (head first 设计模式案例)
*/  
using namespace std;
 
class observer   //观察者公共接口  
{
  public:
         virtual void update(float tmp, float humidity, float pressure) {}    
};
 
class subject    //主题公共接口     问题:成员函数为什么不能用纯虚函数?  
{ public:
  virtual void register_observer(observer o) {}
  virtual void remove_observer(observer o){}
  virtual void notify_observer() {}
};
 
 
 
class display_element   //显示公共接口  
{
  public:
       virtual void display() = 0;    
};
 
 
 
class weatherdata : public subject   //具体主题  
{
  private:
    float temperature;
    float humidity;
    float pressure;
    list<observer*> *observers;
  public:
    weatherdata()
    {
      observers = new list<observer*>;    
    }
    void register_observer(observer* o)  //将观察者注册到观察者列表中  
    {
      (*observers).push_back(o);    
    }
    void remove_observer(observer* o)
    { list<observer*>::iterator it;
      it = find((*observers).begin(),(*observers).end(),o);
        
      (*observers).erase(it);    
    }
    void notify_observer()     //通知观察者  
    { list<observer*>::iterator ite;
      ite = observers->begin();
      for(; ite != observers->end(); ite++)
      {
        
        (*ite)->update(temperature,humidity,pressure);      
      }    
    }    
    void set_measurements(float temperature, float humidity, float pressure)
    {
      this->temperature = temperature;
      this->humidity = humidity;
      this->pressure = pressure;  
      notify_observer();    //更新了随时通知观察者  
    }
    
    
 
};
 
class currentconditiondisplay: public observer, public display_element  //具体观察者 ,同时继承了显示公共接口  
{
  private:
        float temperature;
        float humidity;
        float pressure;
        weatherdata *weatherstation;
  public:
        currentconditiondisplay(weatherdata *weatherstation)
        {
          this->weatherstation = weatherstation;
          weatherstation->register_observer(this);    //是不是因为继承了observer接口才能注册?  
        }
        
        void update(float temperature, float humidity, float pressure)
        {
          this->temperature = temperature;
          this->humidity = humidity;
          this->pressure = pressure;
          display();    
        }
       void display()
       {
         cout <<"current condition: "<< endl;
         cout <<"temperature: " <<temperature<< endl;
         cout <<"humidity:" <<humidity<< endl;
         cout <<"pressure:" << pressure<< endl;    
       }
      
};
 
//  客户端    
int main()  
{
  weatherdata *weather_station = new weatherdata();    // 用new时,一定要记住返回的是指针!  
  currentconditiondisplay *display = new currentconditiondisplay(weather_station);
  weather_station->set_measurements(89.67,33.56,56.98);
  weather_station->set_measurements(11,34.01,39);
  
  system("pause");
  return 0;    
}

#include<iostream>
#include<string>
#include<list>
#include<algorithm>
/*
  气象监控应用问题 (head first 设计模式案例)
*/
using namespace std;

class observer   //观察者公共接口
{
  public:
         virtual void update(float tmp, float humidity, float pressure) {}  
};

class subject    //主题公共接口     问题:成员函数为什么不能用纯虚函数?
{ public:
  virtual void register_observer(observer o) {}
  virtual void remove_observer(observer o){}
  virtual void notify_observer() {}
};

 

class display_element   //显示公共接口
{
  public:
       virtual void display() = 0;  
};

 

class weatherdata : public subject   //具体主题
{
  private:
    float temperature;
    float humidity;
    float pressure;
    list<observer*> *observers;
  public:
    weatherdata()
    {
      observers = new list<observer*>;  
    }
    void register_observer(observer* o)  //将观察者注册到观察者列表中
    {
      (*observers).push_back(o);  
    }
    void remove_observer(observer* o)
    { list<observer*>::iterator it;
      it = find((*observers).begin(),(*observers).end(),o);
      
      (*observers).erase(it);  
    }
    void notify_observer()     //通知观察者
    { list<observer*>::iterator ite;
      ite = observers->begin();
      for(; ite != observers->end(); ite++)
      {
      
        (*ite)->update(temperature,humidity,pressure);    
      }  
    }  
    void set_measurements(float temperature, float humidity, float pressure)
    {
      this->temperature = temperature;
      this->humidity = humidity;
      this->pressure = pressure;
      notify_observer();    //更新了随时通知观察者
    }
  
  

};

class currentconditiondisplay: public observer, public display_element  //具体观察者 ,同时继承了显示公共接口
{
  private:
        float temperature;
        float humidity;
        float pressure;
        weatherdata *weatherstation;
  public:
        currentconditiondisplay(weatherdata *weatherstation)
        {
          this->weatherstation = weatherstation;
          weatherstation->register_observer(this);    //是不是因为继承了observer接口才能注册?
        }
      
        void update(float temperature, float humidity, float pressure)
        {
          this->temperature = temperature;
          this->humidity = humidity;
          this->pressure = pressure;
          display();  
        }
       void display()
       {
         cout <<"current condition: "<< endl;
         cout <<"temperature: " <<temperature<< endl;
         cout <<"humidity:" <<humidity<< endl;
         cout <<"pressure:" << pressure<< endl;  
       }
    
};

//  客户端
int main()
{
  weatherdata *weather_station = new weatherdata();    // 用new时,一定要记住返回的是指针!
  currentconditiondisplay *display = new currentconditiondisplay(weather_station);
  weather_station->set_measurements(89.67,33.56,56.98);
  weather_station->set_measurements(11,34.01,39);
 
  system("pause");
  return 0;  
}

喜欢0 评分0
游客

返回顶部