⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 observer.cpp

📁 深入浅出设计模式部分C++源码。用.net2003调试通过。
💻 CPP
字号:
/*
*观察者模式,用于建立一种一对多依赖关系,当一个对象发生改变时,所有依赖它的对象都得到通知
*/

#include <iostream>
#include <vector>
using namespace std;

/*
*观察者
*/
class Observer
{
public:
	virtual void Update(float temp, float humidity, float pressure) = 0;
};

/*
*目标
*/
class Subject
{
public:
	virtual void RegisterObserver(const Observer *o) = 0;
	virtual void RemoveObserver(const Observer *o) = 0;
	virtual void NotifyObservers() = 0;
};

/*
*灵活改变观察者显示文本的方式
*/
class DisplayElement
{
public:
	virtual void Display() = 0;
};

/*
*目标,负责添加观察者、删除观察者
*/
class WeatherData : public Subject
{
public:
	WeatherData()
		:temperature(0),
		humidity(0),
		pressure(0)
	{
		
	}
	/*
	*注册观察者
	*/
	void RegisterObserver(const Observer *o)
	{
		observers.push_back(const_cast<Observer*>(o));
	}
	/*
	*删除观察者
	*/
	void RemoveObserver(const Observer *o)
	{
		vector<Observer*>::iterator iter;

		for (iter = observers.begin(); iter != observers.end(); iter++)
		{
			if (*iter == o)
			{	
				observers.erase(iter);
				return;
			}
		}

	}
	/*
	*通知观察者信息改变
	*/
	void NotifyObservers()
	{
		vector<Observer*>::iterator iter;

		for (iter = observers.begin(); iter !=  observers.end(); iter++)
		{
			(*iter)->Update(temperature, humidity, pressure);
		}
	}

	void MeasurementsChanged()
	{
		NotifyObservers();
	}

	void SetMeasurements(float t, float h, float p)
	{
		temperature = t;
		humidity = h;
		pressure = p;

		MeasurementsChanged();
	}
private:
	vector<Observer*> observers;
	float temperature;
	float humidity;
	float pressure;
};

/*
*目标
*/
class CurrentConditionsDisplay : public Observer, public DisplayElement
{
public:
	CurrentConditionsDisplay(Subject* wd)
		:weatherData(wd)
	{
		weatherData->RegisterObserver(this);
	}
	void Update(float t, float h, float p)
	{
		temperature = t;
		humidity = h;
		Display();

	}
	void Display()
	{
		cout << "Current condition : " << temperature <<  "F degrees and ";
		cout <<	humidity << "% humidity" << endl;
	}
private:
	float temperature;
	float humidity;
	Subject *weatherData;
};

/*
*目标
*/
class StatisticsDisplay: public Observer, public DisplayElement
{
public:
	StatisticsDisplay(Subject* wd)
		:weatherData(wd)
	{
		weatherData->RegisterObserver(this);
	}
	void Update(float t, float h, float p)
	{
		temperature = t;
		humidity = h;
		Display();
	}
	void Display()
	{
		cout << "Statistics condition : " << temperature <<  "F degrees and ";
		cout <<	humidity << "% humidity" << endl;
	}
private:
	float temperature;
	float humidity;
	Subject *weatherData;
};

/*
*目标
*/
class ForecastDisplay : public Observer, public DisplayElement
{
public:
	ForecastDisplay(Subject* wd)
		:weatherData(wd)
	{
		weatherData->RegisterObserver(this);
	}
	void Update(float t, float h, float p)
	{
		temperature = t;
		humidity = h;
		Display();
	}
	void Display()
	{
		cout << "Forecast condition : " << temperature <<  "F degrees and ";
		cout <<	humidity << "% humidity" << endl;
	}
private:
	float temperature;
	float humidity;
	Subject *weatherData;
};

int main()
{
	WeatherData *wheatherData = new WeatherData;

	CurrentConditionsDisplay *current = new CurrentConditionsDisplay(wheatherData);
	ForecastDisplay *forecast = new ForecastDisplay(wheatherData);
	StatisticsDisplay *statics = new StatisticsDisplay(wheatherData);
	
	cout << "Measure: 1" << endl;
	wheatherData->SetMeasurements(10, 20, 30);
	cout << "Measure: 2" << endl;
	wheatherData->SetMeasurements(30, 20, 30);
	cout << "Measure: 3" << endl;
	wheatherData->SetMeasurements(40, 50, 60);

	
	wheatherData->RemoveObserver(current);

	cout << "Current Condition removed" << endl;
	wheatherData->SetMeasurements(30, 30, 30);

	wheatherData->RemoveObserver(forecast);
	cout << "Forecast removed" << endl;
	wheatherData->SetMeasurements(20, 30, 9);


	delete wheatherData;
	delete current;
	delete forecast;
	delete statics;

	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -