Skip to content

Instantly share code, notes, and snippets.

@gayashanbc
Created July 22, 2018 18:40
Show Gist options
  • Save gayashanbc/50f3783fd931be2180a65e6f66e273cd to your computer and use it in GitHub Desktop.
Save gayashanbc/50f3783fd931be2180a65e6f66e273cd to your computer and use it in GitHub Desktop.
Implementation file of the WeatherData class (Observer Pattern in C++)
//
// 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