码迷,mamicode.com
首页 > 其他好文 > 详细

观察者模式

时间:2015-08-21 15:03:41      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:

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

技术分享

技术分享
#ifndef SUBJECT_H
#define SUBJECT_H

#include "Observer.h"

class Subject
{
public:
    virtual void registerObserver( Observer* o) = 0;
    virtual void removeObserver( Observer* o) = 0;
    virtual void notifyObservers() = 0;
};

#endif
Subject.h
技术分享
#ifndef WEATHERDATA_H
#define WEATHERDATA_H

#include "Subject.h"
#include "Observer.h"
#include <list>

using namespace std;

class WeatherData : public Subject
{
public:
    WeatherData (double temp = 0, double hum = 0, double pre = 0) : temperature(temp), humidity(hum), pressure(pre){};
    void registerObserver( Observer* o ) 
    {
        observers.push_back (o);
    }
    void removeObserver( Observer* o )
    {
        observers.remove(o);
    }
    void notifyObservers()
    {
        for (list<Observer*>::iterator itr = observers.begin(); itr != observers.end(); ++itr)
        {
            (*itr)->update(temperature, humidity, pressure);
        }
    }
    void measurementsChanged()
    {
        notifyObservers();
    }
    void setMeasurements(double temperature, double humidity, double pressure)
    {
        this->temperature = temperature;
        this->humidity = humidity;
        this->pressure = pressure;
        measurementsChanged();
    }
private:
    list<Observer*> observers;
    double temperature;
    double humidity;
    double pressure;
};

#endif
WeatherData.h
技术分享
#ifndef OBSERVER_H
#define OBSERVER_H

class Observer
{
public:
    virtual void update(double temp, double humidity, double pressure) = 0;
};

#endif
Observer.h
技术分享
#ifndef STATISTICSDISPLAY_H
#define STATISTICSDISPLAY_H

#include "Observer.h"
#include "DisplayElement.h"
#include "WeatherData.h"
#include <iostream>

using namespace std;

class  StatisticsDisplay : public Observer, DisplayElement
{
public:
    StatisticsDisplay (Subject* weatherData, double pre = 0) : pressure(pre)
    {
        this->weatherdata = weatherData;
        weatherData->registerObserver(this);
    }
    virtual ~StatisticsDisplay()
    {
        weatherdata->removeObserver(this);
    }
    void update (double temp, double hum, double pre)
    {
        pressure = pre;
        display();
    }
    void display()
    {
        cout<<"Statistics: "<<pressure <<endl;
    }
private:
    Subject* weatherdata;
    double pressure;
};

#endif
StatisticsDisplay.h
技术分享
#ifndef CURRENTCONDITIONSDISPLAY_H
#define CURRENTCONDITIONSDISPLAY_H

#include "Observer.h"
#include "DisplayElement.h"
#include "WeatherData.h"
#include <iostream>

using namespace std;

class  CurrentConditionsDisplay : public Observer, DisplayElement
{
public:
    CurrentConditionsDisplay (Subject* weatherData, double temp = 0, double hum = 0) : temperature(temp), humidity(hum)
    {
        this->weatherdata = weatherData;
        weatherData->registerObserver(this);
    }
    virtual ~CurrentConditionsDisplay()
    {
        weatherdata->removeObserver(this);
    }
    void update (double temp, double hum, double pre)
    {
        temperature = temp;
        humidity = hum;
        display();
    }
    void display()
    {
        cout<<"Current conditions: "<<temperature <<","<<humidity<<endl;
    }
private:
    Subject* weatherdata;
    double temperature;
    double humidity;
};

//#endif
CurrentConditionsDisplay.h
技术分享
#ifndef DISPLAYELEMENT_H
#define DISPLAYELEMENT_H

class DisplayElement
{
public:
    virtual void display() = 0;
};

#endif
DisplayElement
技术分享
#include "WeatherData.h"
#include "StatisticsDisplay.h"
#include "CurrentConditionsDisplay.h"

int main()
{
    WeatherData weatherdata;
    StatisticsDisplay stadisp( &weatherdata );
    CurrentConditionsDisplay currdisp( &weatherdata );;
    weatherdata.setMeasurements(1,2,3);
    return 1;
}
main.cpp

 

观察者模式

标签:

原文地址:http://www.cnblogs.com/lisiyuannnn/p/4744709.html

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