Headfirst设计模式的C++实现——观察者模式(Observer)

本文介绍了如何使用观察者模式实现天气数据更新通知机制,通过WeatherData类管理温度、湿度和气压的测量变化,并注册多个Display观察者进行数据展示。包括CurrentConditionsDisplay、StatisticsDisplay和ForcastDisplay三种不同的展示方式。
摘要由CSDN通过智能技术生成

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( &current_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 }

 

转载于:https://www.cnblogs.com/ren-yu/p/4940925.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值