WeatherData.h
1 #ifndef WEATHERDATA_H_INCLUDED 2 #define WEATHERDATA_H_INCLUDED 3 4 #include <set> 5 #include "Display.h" 6 7 class WeatherData 8 { 9 public:10 void measurementsChanged();11 void registerObserver( Display *p_display );12 void removeObserver( Display *p_display );13 14 private:15 int getTemperature() { return 25; }16 int getHumidity() { return 90; }17 int getPressure() { return 120; }18 19 std::set<Display *> m_p_displays;20 };21 22 #endif // WEATHERDATA_H_INCLUDED
WeatherData.cpp
1 #include "WeatherData.h" 2 3 void WeatherData::measurementsChanged() 4 { 5 for ( std::set<Display *>::iterator it = m_p_displays.begin(); it != m_p_displays.end(); it++ ) 6 { 7 (*it)->update( getTemperature(), getHumidity(), getPressure() ); 8 } 9 }10 11 void WeatherData::registerObserver( Display *p_display )12 {13 m_p_displays.insert( p_display );14 }15 16 void WeatherData::removeObserver( Display *p_display )17 {18 m_p_displays.erase( p_display );19 }
Display.h
1 #ifndef DISPLAY_H_INCLUDED 2 #define DISPLAY_H_INCLUDED 3 4 class Display 5 { 6 public: 7 virtual void update( int temp, int humidity, int pressure ) = 0; 8 }; 9 10 #endif // DISPLAY_H_INCLUDED
CurrentConditionsDisplay.h
1 #ifndef CURRENTCONDITIONSDISPLAY_H_INCLUDED 2 #define CURRENTCONDITIONSDISPLAY_H_INCLUDED 3 4 #include <iostream> 5 #include "Display.h" 6 7 class CurrentConditionsDisplay : public Display 8 { 9 public:10 void update( int temp, int humidity, int pressure ) { std::cout << "CurrentConditionsDisplay " << temp << "-" << humidity << "-" << pressure << std::endl; }11 };12 13 #endif // CURRENTCONDITIONSDISPLAY_H_INCLUDED
StatisticsDisplay.h
1 #ifndef STATISTICSDISPLAY_H_INCLUDED 2 #define STATISTICSDISPLAY_H_INCLUDED 3 4 #include <iostream> 5 #include "Display.h" 6 7 class StatisticsDisplay : public Display 8 { 9 public:10 void update( int temp, int humidity, int pressure ) { std::cout << "StatisticsDisplay " << temp << "-" << humidity << "-" << pressure << std::endl; }11 };12 13 14 #endif // STATISTICSDISPLAY_H_INCLUDED
ForcastDisplay.h
1 #ifndef FORCASTDISPLAY_H_INCLUDED 2 #define FORCASTDISPLAY_H_INCLUDED 3 4 #include <iostream> 5 #include "Display.h" 6 7 class ForcastDisplay : public Display 8 { 9 public:10 void update( int temp, int humidity, int pressure ) { std::cout << "ForcastDisplay " << temp << "-" << humidity << "-" << pressure << std::endl; }11 };12 13 #endif // FORCASTDISPLAY_H_INCLUDED
main.cpp
1 #include "WeatherData.h" 2 #include "CurrentConditionsDisplay.h" 3 #include "StatisticsDisplay.h" 4 #include "ForcastDisplay.h" 5 6 int main() 7 { 8 WeatherData weather_data; 9 10 CurrentConditionsDisplay current_conditions_display;11 StatisticsDisplay statistics_display;12 ForcastDisplay forcast_display;13 14 weather_data.registerObserver( ¤t_conditions_display );15 weather_data.registerObserver( &statistics_display );16 weather_data.registerObserver( &forcast_display );17 18 weather_data.measurementsChanged();19 20 weather_data.removeObserver( &statistics_display );21 22 weather_data.measurementsChanged();23 24 return 0;25 }
原标题:Headfirst设计模式的C++实现——观察者模式(Observer)
关键词:设计模式