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;
}