namespace data
{
class Beans
{
public :
~Beans();
};
};
class WeatherBeans : public data::Beans
{
private :
int _temperature;
int _humidity;
int _pressure;
public :
WeatherBeans(int temperature, int humidity, int pressure);
~WeatherBeans();
int getTemperature();
int getHumidity();
int getPressure();
void showData();
};
#include "data_Beans.h"
data::Beans::~Beans(){}
WeatherBeans::WeatherBeans(int temperature, int humidity, int pressure){
_temperature = temperature;
_humidity = humidity;
_pressure = pressure;
}
WeatherBeans::~WeatherBeans(){
std::cout << "$ destoryed WeatherBeans" << std::endl;
}
int WeatherBeans::getTemperature(){
return _temperature;
}
int WeatherBeans::getHumidity(){
return _humidity;
}
int WeatherBeans::getPressure(){
return _pressure;
}
void WeatherBeans::showData(){
std::cout << "$ 온도 : " << getTemperature() << std::endl;
std::cout << "$ 습도 : " << getHumidity() << std::endl;
std::cout << "$ 압력 : " << getPressure() << std::endl;
}
#include "data_Beans.h"
class DataSource
{
private :
WeatherBeans * m_dataBean;
public :
DataSource(WeatherBeans * bean);
~DataSource();
operator WeatherBeans*() const{
return m_dataBean;
}
void showData();
};
#include "DataSource.h"
DataSource::DataSource(WeatherBeans * bean){
m_dataBean = bean;
}
DataSource::~DataSource(){
std::cout << "$ destroyed DataSource" << std::endl;
delete m_dataBean; m_dataBean = NULL;
}
void DataSource::showData(){
this->operator class WeatherBeans *() -> showData();
}
#include "DataSource.h"
/*
push Observer
*/
class Observer
{
public :
virtual ~Observer();
virtual void update(DataSource * dataSource) = 0;
};
#include "Observer.h"
Observer::~Observer(){
std::cout << "$ destoryed Observer reference" << std::endl;
}
class Observer;
/*
abstract
*/
class Notification
{
protected :
std::vector<Observer *> _observers;
public :
~Notification();
void addObserver(Observer * observer);
Observer * removeObserver(Observer * observer);
virtual void notifyObserver() = 0;
};
#include "Notification.h"
Notification::~Notification(){
std::cout << "$ destroyed Notification reference" << std::endl;
}
void Notification::addObserver(Observer *observer){
std::vector<Observer *>::iterator i;
for(i = _observers.begin(); i!=_observers.end(); ++i){
if(*i == observer) return;
}
_observers.push_back(observer);
}
Observer * Notification::removeObserver(Observer *observer){
std::vector<Observer *>::iterator i;
for(i = _observers.begin(); i!=_observers.end(); ++i){
if(*i == observer){
_observers.erase(i);
return *i;
}
}
}
#include "Observer.h"
#include "Notification.h"
class WeatherData : public Notification
{
private :
int _temperature;
int _humidity;
int _pressure;
DataSource * m_dataSource;
public :
WeatherData();
~WeatherData();
void notifyObserver();
void setMeasurements(int temperature, int humidity, int pressure);
void setMeasurements(DataSource * dataSource);
};
#include "WeatherData.h"
WeatherData::WeatherData(){
}
WeatherData::~WeatherData(){
std::cout << "$ destroyed WeatherData" << std::endl;
}
void WeatherData::setMeasurements(DataSource *dataSource){
m_dataSource = dataSource;
notifyObserver();
}
void WeatherData::setMeasurements(int temperature, int humidity, int pressure){
_temperature = temperature;
_humidity = humidity;
_pressure = pressure;
notifyObserver();
}
void WeatherData::notifyObserver(){
std::vector<Observer *>::iterator i;
for(i = _observers.begin(); i!=_observers.end(); ++i){
(*i) -> update(m_dataSource);
}
}
#include "Observer.h"
#include "WeatherData.h"
class CurrentConditionDisplay : public Observer
{
private :
WeatherData * m_notification;
DataSource * m_dataSource;
int _temperature;
int _humidity;
int _pressure;
public :
CurrentConditionDisplay(WeatherData * notification);
~CurrentConditionDisplay();
#pragma mark -
#pragma mark override by Observer
void update(DataSource *dataSource);
};
#include "CurrentConditionDisplay.h"
CurrentConditionDisplay::CurrentConditionDisplay(WeatherData * notification){
m_notification = notification;
m_notification -> addObserver(this);
}
CurrentConditionDisplay::~CurrentConditionDisplay(){
std::cout << "$ destroyed CurrentConditionDisplay" << std::endl;
}
void CurrentConditionDisplay::update(DataSource *dataSource){
dataSource -> showData();
}
WeatherData * weatherData = new WeatherData();
DataSource * dataSource = new DataSource(new WeatherBeans(0, 1, 2));
CurrentConditionDisplay * currentConditionDisplay = new CurrentConditionDisplay(weatherData);
weatherData -> setMeasurements(dataSource);
weatherData -> removeObserver(currentConditionDisplay);
delete currentConditionDisplay;
delete weatherData;
댓글 없음:
댓글 쓰기