《Head First 设计模式》学习笔记之观察者模式

观察者模式:定义了对象之间的一对多依赖,当一个对象改变状态时,他的所有依赖者都会收到通知并自动更新。最典型的例子就是报社和订阅者。


基本模型:
主题通过保存有所有观察者接口的指针,当自己数据更新时,利用这些指针调用各自观察者的具体更新方法,完成更新。


简单实现:
#include<iostream>
#include<vector>
#include<algorithm>
#include<time.h>
using namespace std;
class Observer
{
public:
	virtual void update(float t, float h, float s){};
	virtual void show(){};
};

class ObserverMin :public Observer
{
public:
	ObserverMin();
	void update(float t, float h, float s);
	void show();
private:
	float _temp;
	float _humity;
	float _stress;
};
ObserverMin::ObserverMin() :_temp(INT_MAX), _humity(INT_MAX), _stress(INT_MAX)
{

}
void ObserverMin::update(float t, float h, float s)
{
	if (t < _temp)
		_temp = t;
	if (h < _humity)
		_humity = h;
	if (s < _stress)
		_stress = s;
}
void ObserverMin::show()
{
	cout << "Min:" << _temp << " " << _humity << " " << _stress << endl;
}

class ObserverMax :public Observer
{
public:
	ObserverMax();
	void update(float t, float h, float s);
	void show();
private:
	float _temp;
	float _humity;
	float _stress;
};
ObserverMax::ObserverMax() :_temp(0), _humity(0), _stress(0)
{

}
void ObserverMax::update(float t, float h, float s)
{
	if (t > _temp)
		_temp = t;
	if (h > _humity)
		_humity = h;
	if (s > _stress)
		_stress = s;
}
void ObserverMax::show()
{
	cout << "Max:" << _temp << " " << _humity << " " << _stress << endl;
}

class ObserverAverage :public Observer
{
public:
	ObserverAverage();
	void update(float t, float h, float s);
	void show();
private:
	int _count;
	float _temp;
	float _humity;
	float _stress;
};
ObserverAverage::ObserverAverage() :_temp(0), _humity(0), _stress(0), _count(0)
{

}
void ObserverAverage::update(float t, float h, float s)
{
	_temp=(_temp*_count + t) / (_count + 1);
	_humity = (_humity*_count + h) / (_count + 1);
	_stress = (_stress*_count + s) / (_count + 1);
	_count++;
}
void ObserverAverage::show()
{
	cout << "Average:" << _temp << " " << _humity << " " << _stress << endl;
}

class Subject
{
public:
	Subject();
	Subject(float t, float h, float s);
	~Subject();
	void registerObserver(Observer *observer);
	void removeObserver(Observer *observer);
	void setData(float t,float h,float s);
private:
	float _temp;
	float _humity;
	float _stress;
	vector<Observer*> _observerList;
};

Subject::Subject() :_temp(0), _humity(0), _stress(0)
{
	_observerList.clear();
}

Subject::Subject(float t, float h, float s) : _temp(t), _humity(h), _stress(s)
{
	_observerList.clear();
}

void Subject::setData(float t, float h, float s)
{
	cout << "Sunject::setData()" << endl;
	for (auto i : _observerList)
	{
		if (i)
		{
			i->update(t, h, s);
			i->show();
		}
	}
}
void Subject::registerObserver(Observer *observer)
{
	for (auto i : _observerList)
	{
		if (i == observer)
			return;
	}
	_observerList.push_back(observer);
}
void Subject::removeObserver(Observer *observer)
{
	auto it=find(_observerList.begin(), _observerList.end(), observer);
	if (it != _observerList.end())
	{
		_observerList.erase(it);
	}
}

int main()
{
	auto begin = clock();
	auto subject = new Subject();
	auto ob1 = new ObserverMin();
	auto ob2 = new ObserverMax();
	auto ob3 = new ObserverAverage();
	int n = 100;
	subject->registerObserver(ob1);
	subject->registerObserver(ob2);
	subject->registerObserver(ob3);
	subject->setData(10, 10, 10);
	subject->setData(11, 11, 11);
	subject->removeObserver(ob1);
	subject->setData(9, 12, 31.3);
	subject->registerObserver(ob1);
	subject->setData(11,3,50);
	auto end = clock();
	cout << "TimeDelta : " << (end - begin) <<"ms" << endl; 
	system("pause");
	return 0;
}


输出:




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值