design_pattern_observer

来源:互联网 发布:淘宝背后的老板是谁 编辑:程序博客网 时间:2024/05/19 08:37

This post implements observer pattern in <Head First Design Pattern>. Observer Pattern defines a one-to-many dependency between objects so that when one object changes state, all of its dependents are notified and updated automatically. The basic structure is given by

We need to implement main four classes,

1. First is the subject abstract class. Subject manages Observers, add, remove observers and update information.

2. Then we have Observer abstract class. The basic function for observer class is to receive and update information.

3. ConcreteObserver inherits from Observer, except for receive and update information, it defines some specific method to deal with information.

4. ConcretSubject inherits from Subject, doing add, remove, update, and other stuffs, is an integral work interface(station).

class Subject {public:    virtual void registerObserver(Observer* o) = 0;    virtual void removeObserver(Observer* o) = 0;    virtual void notifyObserver() = 0;};class Observer{public:    virtual void update(double temp, double humidity, double pressure) const = 0;};

Specifically, for weather station problem in the book, we have class structure

Weather Data implements the subject abstract class. Its responsibility is to manage observers,  which are CurrentConditions, StatsitcsDisply and ThirdPartyDisplay.

class WeatherData : public Subject{public:    WeatherData(){        this->temperature = 0;        this->humidity = 0;        this->pressure = 0;    }    WeatherData(double temp, double humid, double pres){        this->temperature = temp;        this->humidity = humid;        this->pressure = pres;    }    ~WeatherData(){        for(std::list<Observer*>::iterator it=observers.begin;observers.end() != it; ++it){            delete *it;            *it = NULL;        }    }    virtual void registerObserver(Observer* o) {observers.push_back(o);}virtual void removeObserver(Observer* o) {observers.remove(o);}    virtual void notifyObservers() {for (std::list<Observer*>::iterator it = observers.begin(); observers.end() != it; ++it) {Observer* observer = *it;observer->update(temperature, humidity, pressure);}}virtual void measurementsChanged() {notifyObservers();}virtual void setMeasurements(double temperature, double humidity, double pressure) {this->temperature = temperature;this->humidity = humidity;this->pressure = pressure;measurementsChanged();}virtual float getTemperature() {return temperature;}virtual float getHumidity() {return humidity;}virtual float getPressure() {return pressure;}protected:    double temperature;double humidity;double pressure;static std::list<Observer*> observers;};

The WeatherData contains basic data and information of a list of observers. It can add and remove them. The list should bestatic?  We further implement one Observer instance, CurrentConditionsDisplay.

class CurrentConditionDisplay : public Observer{public:    CurrentConditionDisplay(){        temperature = 0;        humidity = 0;        pressure = 0;        //weatherdata = NULL;    }    CurrentConditionDisplay(Subject* wd){        this->weatherdata = wd;        weatherdata->registerObserver(this);    }    ~CurrentConditionDisplay(){        delete weatherdata;        weatherdata = NULL;    }    virtual void update(double temp, double humid, double pres) {this->temperature = temp;this->humidity = humid;this->pressure = pres;this->display();}void display(){        std::cout << "Temperature|Humidity|Pressure:" << temperature << "|" << humidity  << "|"               << pressure << std::endl;}private:    double temperature;    double humidity;double pressure;Subject* weatherdata;};
main function is given by

#include <iostream>#include <list>#include <./weatherstation.hpp>int main(){    WeatherData* wd = new WeatherData();    CurrentConditionDisplay* cd = new CurrentConditionDisplay(wd);    wd->setMeasurements(80, 65, 30);    wd->setMeasurements(82, 62, 29);    wd->setMeasurements(79, 60, 31);    return 0;}
The result is




0 0
原创粉丝点击