C + + implementation of the headfirst design pattern-Observer mode (OBSERVER)

Source: Internet
Author: User

WeatherData.h

1 #ifndef weatherdata_h_included2 #defineweatherdata_h_included3 4#include <Set>5#include"Display.h"6 7 classWeatherdata8 {9  Public:Ten     voidmeasurementschanged (); One     voidRegisterobserver (Display *p_display); A     voidRemoveobserver (Display *p_display); -  - Private: the     intGettemperature () {return  -; } -     intGethumidity () {return  -; } -     intGetpressure () {return  -; } -  +STD::Set<display *>m_p_displays; - }; +  A #endif //weatherdata_h_included

WeatherData.cpp

1#include"WeatherData.h"2 3 voidweatherdata::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 }Ten  One voidWeatherdata::registerobserver (Display *p_display) A { - M_p_displays.insert (p_display); - } the  - voidWeatherdata::removeobserver (Display *p_display) - { - m_p_displays.erase (p_display); +}

Display.h

1 #ifndef display_h_included2 #definedisplay_h_included3 4 classDisplay5 {6  Public:7     Virtual voidUpdateintTempintHumidity,intpressure) =0;8 };9 Ten #endif //display_h_included

CurrentConditionsDisplay.h

1 #ifndef currentconditionsdisplay_h_included2 #definecurrentconditionsdisplay_h_included3 4#include <iostream>5#include"Display.h"6 7 classCurrentconditionsdisplay: PublicDisplay8 {9  Public:Ten     voidUpdateintTempintHumidity,intpressure) {Std::cout <<"Currentconditionsdisplay"<< Temp <<"-"<< Humidity <<"-"<< pressure <<Std::endl;} One }; A  - #endif //currentconditionsdisplay_h_included

StatisticsDisplay.h

1 #ifndef statisticsdisplay_h_included2 #definestatisticsdisplay_h_included3 4#include <iostream>5#include"Display.h"6 7 classStatisticsdisplay: PublicDisplay8 {9  Public:Ten     voidUpdateintTempintHumidity,intpressure) {Std::cout <<"Statisticsdisplay"<< Temp <<"-"<< Humidity <<"-"<< pressure <<Std::endl;} One }; A  -  - #endif //statisticsdisplay_h_included

ForcastDisplay.h

1 #ifndef forcastdisplay_h_included2 #defineforcastdisplay_h_included3 4#include <iostream>5#include"Display.h"6 7 classForcastdisplay: PublicDisplay8 {9  Public:Ten     voidUpdateintTempintHumidity,intpressure) {Std::cout <<"Forcastdisplay"<< Temp <<"-"<< Humidity <<"-"<< pressure <<Std::endl;} One }; A  - #endif //forcastdisplay_h_included

Main.cpp

1#include"WeatherData.h"2#include"CurrentConditionsDisplay.h"3#include"StatisticsDisplay.h"4#include"ForcastDisplay.h"5 6 intMain ()7 {8 Weatherdata Weather_data;9 Ten Currentconditionsdisplay Current_conditions_display; One Statisticsdisplay Statistics_display; A Forcastdisplay Forcast_display; -  -Weather_data.registerobserver (&current_conditions_display); theWeather_data.registerobserver (&statistics_display); -Weather_data.registerobserver (&forcast_display); -  - weather_data.measurementschanged (); +  -Weather_data.removeobserver (&statistics_display); +  A weather_data.measurementschanged (); at  -     return 0; -}

C + + implementation of the headfirst design pattern-Observer mode (OBSERVER)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.