程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 觀察者模式 C++ 實現

觀察者模式 C++ 實現

編輯:C++入門知識

[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;   
}


 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved