观察者模式:定义了对象之间的一对多依赖,这样一来,当一个对象改变状态时,他的所有依赖者都会收到通知并自动更新。
Java代码
c++代码
#pragma once #include "Observer.h" class Subject { public: virtual void registerObserver(Observer* o) = 0; virtual void removeObserver(Observer* o) = 0; virtual void notifyObserver() = 0; }; //----------------------------------------- #pragma once #include <iostream> #include <string> using namespace std; class Observer { public: virtual void update(string arg) = 0; }; //----------------------------------------- #pragma once #include "Observer.h" #include "Subject.h" class CurrentConditionsDisplay : public Observer { Subject* subject; public: CurrentConditionsDisplay(Subject* sub) { subject = sub; subject->registerObserver(this); } ~CurrentConditionsDisplay() { } void update(string arg) { cout << "CurrentConditionsDisplay arg:" << arg << '\n'; } }; //----------------------------------------- #pragma once #include "Observer.h" #include "Subject.h" class ForecastDisplay : public Observer { Subject* subject; public: ForecastDisplay(Subject* sub) { subject = sub; subject->registerObserver(this); } ~ForecastDisplay() { } void update(string arg) { cout << "ForecastDisplay arg:" << arg << '\n'; } }; //----------------------------------------- #pragma once #include "Observer.h" #include "Subject.h" class StatisticsDisplay : public Observer { Subject* subject; public: StatisticsDisplay(Subject* sub) { subject = sub; subject->registerObserver(this); } ~StatisticsDisplay() { } void update(string arg) { cout << "StatisticsDisplay arg:" << arg << '\n'; } }; //----------------------------------------- #pragma once #include "Subject.h" #include <string> #include <vector> #include <algorithm> using namespace std; class WeatherData : public Subject { string temperature; string humidity; string pressure; vector<Observer*> observers; public: WeatherData() { } ~WeatherData() { } void registerObserver(Observer* o) { observers.push_back(o); } void removeObserver(Observer* o) { observers.erase(remove(observers.begin(), observers.end(), o)); } void notifyObserver() { for (int i = 0; i < observers.size(); i++) { Observer* pObj = observers.at(i); string arg = this->temperature + ";" + this->humidity + ";" + this->pressure; pObj->update(arg); } } void measurementsChanged() { notifyObserver(); } void setMeasurements(float temperature, float humidity, float pressure) { this->temperature = to_string(temperature); this->humidity = to_string(humidity); this->pressure = to_string(pressure); measurementsChanged(); } }; //----------------------------------------- #include <iostream> #include "CurrentConditionsDisplay.h" #include "ForecastDisplay.h" #include "StatisticsDisplay.h" #include "WeatherData.h" using namespace std; int main() { WeatherData data; CurrentConditionsDisplay d1(&data); ForecastDisplay d2(&data); StatisticsDisplay d3(&data); data.setMeasurements(13.2, 23, 31); data.setMeasurements(15.2, 28, 71); data.removeObserver(&d2); data.setMeasurements(17.2, 21, 61); return 0; } //-----------------------------------------View Code
****
标签:HeadFirst,include,string,Observer,void,观察者,observers,设计模式,data From: https://www.cnblogs.com/htj10/p/16895798.html