码迷,mamicode.com
首页 > 编程语言 > 详细

【C++实现】HeadFirst设计模式之观察者模式

时间:2014-08-02 15:33:03      阅读:252      评论:0      收藏:0      [点我收藏+]

标签:设计模式   c++   观察者模式   

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

一方可称为主题(subject),另一方可称为观察者(observer),一个主题可拥有多个观察者。当主题的数据有变动时,就会通知所有订阅了它的观察者。在这之后的操作有两种:

  • 推:主题直接将数据推送给所有观察者。
  • 拉:主题通知了所有观察者后,每个观察者再根据自己的需要从主题拉取所需数据。

下面是我用C++实现的代码,其中的update方法采用了“拉”的方式从主题获取数据。

//MyObserver.h
#ifndef MYOBSERVER_H_INCLUDED
#define MYOBSERVER_H_INCLUDED

#endif // MYOBSERVER_H_INCLUDED

#include<iostream>
#include<vector>
#define TRUE 1;
#define FALSE 0;
using namespace std;

class MyObserver{
    public:
        virtual void update(){
            cout<<"I'm update method ofbase class.\n";
        }
        virtual void display(){
            cout<<"I'm display method ofbase class.\n";
        }
};

class MyObservable{
    protected:
        vector<MyObserver*>observers;
    public:
        //MyObservable();
        ~MyObservable(){
            observers.clear();
        }
        int addObserver(MyObserver& obs){
            observers.push_back(&obs);
            return TRUE;
        }
        int deleteObserver(MyObserver& obs){
            for(vector<MyObserver*>::iterator it = observers.begin();it != observers.end();++it){
                if((*it) == &obs){
                    observers.erase(it);
                    return TRUE;
                }
            }
            return FALSE;
        }
        int notifyObservers(){
            for(vector<MyObserver*>::iterator it = observers.begin();it != observers.end();++it){
                (*it)->update();
            }
            return TRUE;
        }

};

//main.cpp
#include <iostream>
#include "MyObserver.h"
using namespace std;

class WeatherData:public MyObservable{
    private:
        float temperature;
        float humidity;
        float pressure;
    public:
        void setMeasurements(float temperature,float humidity,float pressure){
            this->temperature=temperature;
            this->humidity=humidity;
            this->pressure=pressure;
            measurementsChanged();
        }
        void measurementsChanged(){
            notifyObservers();
        }
        float getTemperature(){
            return temperature;
        }
        float getHumidity(){
            return humidity;
        }
        float getPressure(){
            return pressure;
        }
};

class CurrentDisplay:public MyObserver{
    private:
        WeatherData* weatherData;
        float temperature;
        float humidity;
        float pressure;
    public:
        CurrentDisplay(WeatherData& weatherData){
            this->weatherData=&weatherData;
            this->weatherData->addObserver(*this);
        }
        CurrentDisplay(){
            cout<<"You have to give a parameter.\n";
        }
        ~CurrentDisplay(){
            weatherData->deleteObserver(*this);
        }
        void update(){
            temperature=weatherData->getTemperature();
            humidity=weatherData->getHumidity();
            pressure=weatherData->getPressure();
            display();
        }
        void display(){
            cout<<"temperature:"<<temperature<<"\nhumidity:"<<humidity<<"\npressure:"<<pressure<<endl;
        }
};
int main()
{
    WeatherData weatherdata;
    CurrentDisplay currentdisplay(weatherdata);
    weatherdata.setMeasurements(80,65,30.4);
    weatherdata.setMeasurements(82,70,29.2);
    weatherdata.setMeasurements(78,90,29.2);
    currentdisplay.~CurrentDisplay();
    CurrentDisplay test2(weatherdata);
    weatherdata.setMeasurements(78,912,29.2);
    return 0;
}


【C++实现】HeadFirst设计模式之观察者模式,布布扣,bubuko.com

【C++实现】HeadFirst设计模式之观察者模式

标签:设计模式   c++   观察者模式   

原文地址:http://blog.csdn.net/tigerstudent/article/details/38348121

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!