From 8170c932557a96a0b1f21e8af30a20d14992210e Mon Sep 17 00:00:00 2001 From: Alexander Date: Fri, 11 Nov 2022 03:03:18 +0300 Subject: [PATCH] observer CurrentConditionsDisplay --- observer/app.d | 14 ++++++++ observer/currentconditionsdisplay.d | 31 ++++++++++++++++ observer/displayelement.d | 6 ++++ observer/observer.d | 6 ++++ observer/subject.d | 9 +++++ observer/weatherdata.d | 55 +++++++++++++++++++++++++++++ 6 files changed, 121 insertions(+) create mode 100644 observer/app.d create mode 100644 observer/currentconditionsdisplay.d create mode 100644 observer/displayelement.d create mode 100644 observer/observer.d create mode 100644 observer/subject.d diff --git a/observer/app.d b/observer/app.d new file mode 100644 index 0000000..9ffcf5e --- /dev/null +++ b/observer/app.d @@ -0,0 +1,14 @@ +module observer.app; + +import observer.weatherdata; +import observer.currentconditionsdisplay; + +void main() +{ + WeatherData weatherData = new WeatherData(); + CurrentConditionsDisplay currentDisplay = new CurrentConditionsDisplay(weatherData); + + weatherData.setMeasurements(80, 65, 30.4f); + weatherData.setMeasurements(82, 70, 29.2f); + weatherData.setMeasurements(78, 90, 29.2f); +} diff --git a/observer/currentconditionsdisplay.d b/observer/currentconditionsdisplay.d new file mode 100644 index 0000000..815d082 --- /dev/null +++ b/observer/currentconditionsdisplay.d @@ -0,0 +1,31 @@ +module observer.currentconditionsdisplay; +import observer.displayelement; +import observer.observer; +import observer.weatherdata; +import std.stdio : writeln; +import std.format : format; + +class CurrentConditionsDisplay : Observer, DisplayElement +{ +private: + float temperature; + float humidity; + WeatherData weatherData; +public: + this(WeatherData weatherData) + { + weatherData.registerObserver(this); + } + + override void update(float temperature, float humidity, float pressure) + { + this.temperature = temperature; + this.humidity = humidity; + display(); + } + + override void display() + { + writeln("Current conditions: %3.1f".format(temperature), "F degrees and %3.1f%% humidity".format(humidity)); + } +} diff --git a/observer/displayelement.d b/observer/displayelement.d new file mode 100644 index 0000000..5154dfc --- /dev/null +++ b/observer/displayelement.d @@ -0,0 +1,6 @@ +module observer.displayelement; + +interface DisplayElement +{ + void display(); +} diff --git a/observer/observer.d b/observer/observer.d new file mode 100644 index 0000000..a4e5e08 --- /dev/null +++ b/observer/observer.d @@ -0,0 +1,6 @@ +module observer.observer; + +interface Observer +{ + void update(float temp, float humidity, float pressure); +} diff --git a/observer/subject.d b/observer/subject.d new file mode 100644 index 0000000..668feb0 --- /dev/null +++ b/observer/subject.d @@ -0,0 +1,9 @@ +module observer.subject; +import observer.observer; + +interface Subject +{ + void registerObserver(Observer o); + void removeObserver(Observer o); + void notifyObservers(); +} diff --git a/observer/weatherdata.d b/observer/weatherdata.d index e6df941..f097f47 100644 --- a/observer/weatherdata.d +++ b/observer/weatherdata.d @@ -1,2 +1,57 @@ module observer.weatherdata; +import observer.subject; +import observer.observer; +import std.algorithm : remove, countUntil; +class WeatherData : Subject +{ +private: + Observer[] observers; + float temperature, humidity, pressure; +public: + override void registerObserver(Observer o) + { + observers ~= o; + } + + override void removeObserver(Observer o) + { + observers.remove(observers.countUntil(o)); + } + + override void notifyObservers() + { + foreach (Observer o; observers) + { + o.update(temperature, humidity, pressure); + } + } + + void measurementsChanged() + { + notifyObservers(); + } + + void setMeasurements(float temperature, float humidity, float pressure) + { + this.temperature = temperature; + this.humidity = humidity; + this.pressure = pressure; + measurementsChanged(); + } + + @property float getTemperature() + { + return temperature; + } + + @property float getHumidity() + { + return humidity; + } + + @property float getPressure() + { + return pressure; + } +}