Created
July 22, 2018 18:40
-
-
Save gayashanbc/50f3783fd931be2180a65e6f66e273cd to your computer and use it in GitHub Desktop.
Implementation file of the WeatherData class (Observer Pattern in C++)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Created by shan on 4/7/17. | |
// | |
#include "WeatherData.hpp" | |
void WeatherData::registerObserver(Observer *observer) { | |
observers.push_back(observer); | |
} | |
void WeatherData::removeObserver(Observer *observer) { | |
// find the observer | |
auto iterator = std::find(observers.begin(), observers.end(), observer); | |
if (iterator != observers.end()) { // observer found | |
observers.erase(iterator); // remove the observer | |
} | |
} | |
void WeatherData::notifyObservers() { | |
for (Observer *observer : observers) { // notify all observers | |
observer->update(temp, humidity, pressure); | |
} | |
} | |
void WeatherData::setState(float temp, float humidity, float pressure) { | |
this->temp = temp; | |
this->humidity = humidity; | |
this->pressure = pressure; | |
std::cout << std::endl; | |
notifyObservers(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment