标签:
观察者模式:定义了对象之间一对多依赖,这样一来,当一个对象改变状态时,它的所有依赖者都会收到通知并自动更新。
#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
#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
#ifndef OBSERVER_H #define OBSERVER_H class Observer { public: virtual void update(double temp, double humidity, double pressure) = 0; }; #endif
#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
#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
#ifndef DISPLAYELEMENT_H #define DISPLAYELEMENT_H class DisplayElement { public: virtual void display() = 0; }; #endif
#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; }
标签:
原文地址:http://www.cnblogs.com/lisiyuannnn/p/4744709.html