《ASCE1885的设计模式》---观察者模式 (二)

2014-11-24 12:17:26 · 作者: · 浏览: 4
blic DisplayBoardInterface

{

public:

CurrentConditionBoard(WeatherDataInterface& a):m_data(a)

{

m_data.registerob(this);

}

void show()

{

cout<<"_____CurrentConditionBoard_____"<

cout<<"humidity: "<

cout<<"temperature: "<

cout<<"pressure: "<

cout<<"_______________________________"<

}

void update(float h, float t, float p)

{

m_h = h;

m_t = t;

m_p = p;

}

private:

float m_h;

float m_t;

float m_p;

WeatherDataInterface& m_data;

};

// A Concrete Observer

class StatisticBoard : public ObserverBoardInterface, public DisplayBoardInterface

{

public:

StatisticBoard(WeatherDataInterface& a):m_maxt(-1000),m_mint(1000),m_avet(0),m_count(0),m_data(a)

{

m_data.registerob(this);

}

void show()

{

cout<<"________StatisticBoard_________"<

cout<<"lowest temperature: "<

cout<<"highest temperature: "<

cout<<"average temperature: "<

cout<<"_______________________________"<

}

void update(float h, float t, float p)

{

++m_count;

if (t>m_maxt)

{

m_maxt = t;

}

if (t

{

m_mint = t;

}

m_avet = (m_avet * (m_count-1) + t)/m_count;

}

private:

float m_maxt;

float m_mint;

float m_avet;

int m_count;

WeatherDataInterface& m_data;

};

int main(int argc, char *argv[])

{

ParaWeatherData * wdata = new ParaWeatherData;

CurrentConditionBoard* currentB = new CurrentConditionBoard(*wdata);

StatisticBoard* statisticB = new StatisticBoard(*wdata);

wdata->SensorDataChange(10.2, 28.2, 1001);

wdata->SensorDataChange(12, 30.12, 1003);

wdata->SensorDataChange(10.2, 26, 806);

wdata->SensorDataChange(10.3, 35.9, 900);

wdata->removeob(currentB);

wdata->SensorDataChange(100, 40, 1900);

delete statisticB;

delete currentB;

delete wdata;

return 0;

}

摘自 ASCE1885