Observer

C++ Code:

#include <iostream>
#include <map>
#include <vector>
#include <string>

class AbstractObserver
{
public:
	virtual int	 GetObserverId(void) const = 0;
	virtual void UpdateMethod(int temperature, int humidity) const = 0;
};

class AbstractSubject
{
public:
	virtual bool RegisterObserver(AbstractObserver* observerPtr) = 0;
	virtual bool RemoveObserver(AbstractObserver* observerPtr) = 0;
	virtual void NotifyObservers(int t, int h) = 0;
};

class Student : public AbstractObserver
{
private:
	int _observerId;

public:
	Student(int id) : _observerId(id) {}
	virtual int  GetObserverId(void) const
	{
		return _observerId;
	}
	virtual void UpdateMethod(int temperature, int humidity) const
	{
		std::cout << "Student " << this->_observerId \
			<< " " << temperature << " " << humidity << std::endl;
	}
};

class Teacher : public AbstractObserver
{
private:
	int _observerId;

public:
	Teacher(int id) : _observerId(id) {}

	virtual int  GetObserverId(void) const
	{
		return _observerId;
	}
	virtual void UpdateMethod(int temperature, int humidity) const
	{
		std::cout << "Teacher " << this->_observerId \
			<< " " << temperature << " " << humidity << std::endl;
	}
};

class WeatherBoard : public AbstractSubject
{
private:
	std::map<int, AbstractObserver* >	_ObserverKeyIdValueAddress;

public:

	virtual bool RegisterObserver(AbstractObserver* observerPtr)
	{
		int obId = observerPtr->GetObserverId();
		std::map<int, AbstractObserver*>::iterator rst = \
			this->_ObserverKeyIdValueAddress.find(obId);
		if( rst != this->_ObserverKeyIdValueAddress.end() )
			return false;

		this->_ObserverKeyIdValueAddress.insert(\
			std::map<int, AbstractObserver*>::value_type(obId, observerPtr));
		return true;
	}

	virtual bool RemoveObserver(AbstractObserver* observerPtr)
	{
		int obId = observerPtr->GetObserverId();
		std::map<int, AbstractObserver*>::iterator rst = \
			this->_ObserverKeyIdValueAddress.find(obId);
		if( rst == this->_ObserverKeyIdValueAddress.end() )
			return false;
		this->_ObserverKeyIdValueAddress.erase( rst ); 
		return true;
	}

	virtual void NotifyObservers(int t, int h)
	{
		std::map<int, AbstractObserver*>::iterator it;
		for( it = this->_ObserverKeyIdValueAddress.begin(); \
			it != this->_ObserverKeyIdValueAddress.end(); ++ it)
			it->second->UpdateMethod(t, h);
	}
};

//气象台
class Meteorological
{
private:
	int					_temperature;
	int					_humidity;
	AbstractSubject*	_subjectPtr;

public:
	Meteorological(AbstractSubject* ptr) : \
		_temperature(0), _humidity(0), _subjectPtr( ptr ) {}

	void UpdateTempature(int t)
	{
		_temperature = t;
		_subjectPtr->NotifyObservers(_temperature, _humidity);
	}

	void UpdateHumidity(int h)
	{
		_humidity = h;
		_subjectPtr->NotifyObservers(_temperature, _humidity);
	}
};

int main(void)
{
	//定义需要订阅天气板的人
	Student fedora(1);
	Student ubuntu(2);
	Teacher opensuse(3);
	Teacher arch(4);

	//天气板编辑社
	WeatherBoard wb;

	//订阅天气报告
	wb.RegisterObserver(&fedora);
	wb.RegisterObserver(&ubuntu);
	wb.RegisterObserver(&opensuse);
	wb.RegisterObserver(&arch);

	//气象台
	Meteorological ml(&wb);
	ml.UpdateHumidity(10);

	//arch 退订天气报告
	if( false == wb.RemoveObserver(&arch) )
		std::cout << "remove error." << std::endl;

	ml.UpdateTempature(10);

	std::cin.get();
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值