Skip to content

Commit

Permalink
use pattern 'Observer' final
Browse files Browse the repository at this point in the history
  • Loading branch information
Kudryashov Dmitrii committed Nov 3, 2021
1 parent e518edb commit 8857428
Showing 1 changed file with 34 additions and 27 deletions.
61 changes: 34 additions & 27 deletions WeatherData/weatherdata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@ using std::vector;

class Observer {
public:
virtual void update(float temp, float humidity, float pressure) = 0;
virtual void update() = 0;
};

class Subject {
public:
virtual void registerObserver(Observer* observer) = 0;
virtual void removeObserver(Observer* observer) = 0;
virtual void notifyObservers() = 0;

[[nodiscard]] virtual float getTemperature() const = 0;
[[nodiscard]] virtual float getHumidity() const = 0;
[[nodiscard]] virtual float getPressure() const = 0;
};

class DisplayElement {
Expand All @@ -34,10 +38,9 @@ class CurrentConditionsDisplay : public Observer, DisplayElement {
cout << "Current conditions: " << temperature_ << " C degrees and " << humidity_ << "% humidity" << '\n';
}

void update(float temp, float humidity, float pressure) override {
temperature_ = temp;
humidity_ = humidity;
(void)pressure;
void update() override {
temperature_ = weatherData_->getTemperature();
humidity_ = weatherData_->getHumidity();
display();
}

Expand All @@ -59,21 +62,20 @@ class StatisticsDisplay : public Observer, DisplayElement {
cout << "Avg/Max/Min: " << average_temp_ << '/' << max_temp_ << '/' << min_temp_ << '\n';
}

void update(float temp, float humidity, float pressure) override {
temperature_ = temp;
humidity_ = humidity;
pressure_ = pressure;
temps_.push_back(temp);
if (temp > max_temp_) {
max_temp_ = temp;
void update() override {
float temperature_ = weatherData_->getTemperature();

temps_.push_back(temperature_);
if (temperature_ > max_temp_) {
max_temp_ = temperature_;
}

if (temp < min_temp_) {
min_temp_ = temp;
if (temperature_ < min_temp_) {
min_temp_ = temperature_;
}
float sum_temp = 0.0;
for (float temperature: temps_) {
sum_temp += temperature;
for (float temp : temps_) {
sum_temp += temp;
}
if (!temps_.empty()) {
average_temp_ = sum_temp / temps_.size();
Expand All @@ -85,10 +87,6 @@ class StatisticsDisplay : public Observer, DisplayElement {
private:
Subject* weatherData_;

float temperature_ = 0.0;
float humidity_ = 0.0;
float pressure_ = 0.0;

float min_temp_ = 273;
float max_temp_ = -273;
float average_temp_ = 0.0;
Expand All @@ -106,17 +104,14 @@ class ForecastDisplay : public Observer, DisplayElement {
cout << "Tomorrow will be a good day (50%)" << '\n';
}

void update(float temp, float humidity, float pressure) override {
(void)temp;
(void)humidity;
pressure_ = pressure;
void update() override {
pressures_.push_back(weatherData_->getPressure());
display();
}

private:
Subject* weatherData_;

float pressure_ = 0.0;
vector<float> pressures_;
};

class WeatherData : public Subject {
Expand All @@ -129,7 +124,7 @@ class WeatherData : public Subject {
}
void notifyObservers() override {
for (auto* observer : observers_) {
observer->update(temperature_, humidity_, pressure_);
observer->update();
}
}

Expand All @@ -145,6 +140,18 @@ class WeatherData : public Subject {
measurementsChanged();
}

[[nodiscard]] float getTemperature() const override {
return temperature_;
}

[[nodiscard]] float getHumidity() const override {
return humidity_;
}

[[nodiscard]] float getPressure() const override {
return pressure_;
}

private:
set<Observer*> observers_;
float temperature_ = 0.0;
Expand Down

0 comments on commit 8857428

Please sign in to comment.