From 850cb17d02c5fe772e4c981cda959e054e52dc73 Mon Sep 17 00:00:00 2001 From: Alexander Zhirov Date: Fri, 11 Nov 2022 10:22:38 +0300 Subject: [PATCH] notifyObservers first variant --- observer/app.d | 5 ++++- observer/heatindexdisplay.d | 38 +++++++++++++++++++++++++++++++++++++ observer/weatherdata.d | 4 +++- 3 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 observer/heatindexdisplay.d diff --git a/observer/app.d b/observer/app.d index 9ffcf5e..9b44bc3 100644 --- a/observer/app.d +++ b/observer/app.d @@ -1,14 +1,17 @@ module observer.app; - import observer.weatherdata; import observer.currentconditionsdisplay; +import observer.heatindexdisplay; void main() { WeatherData weatherData = new WeatherData(); CurrentConditionsDisplay currentDisplay = new CurrentConditionsDisplay(weatherData); + HeatIndexDisplay heatIndexDisplay = new HeatIndexDisplay(weatherData); weatherData.setMeasurements(80, 65, 30.4f); weatherData.setMeasurements(82, 70, 29.2f); + weatherData.removeObserver(heatIndexDisplay); weatherData.setMeasurements(78, 90, 29.2f); + weatherData.setMeasurements(81, 72, 29.5f); } diff --git a/observer/heatindexdisplay.d b/observer/heatindexdisplay.d new file mode 100644 index 0000000..eedc621 --- /dev/null +++ b/observer/heatindexdisplay.d @@ -0,0 +1,38 @@ +module observer.heatindexdisplay; +import observer.displayelement; +import observer.observer; +import observer.weatherdata; +import std.stdio : writeln; + +class HeatIndexDisplay : Observer, DisplayElement +{ +private: + float heatIndex; + WeatherData weatherData; + + float computeHeatIndex(float t, float rh) + { + return ((16.923 + (0.185212 * t) + (5.37941 * rh) - (0.100254 * t * rh) + (0.00941695 * (t * t)) + + (0.00728898 * (rh * rh)) + (0.000345372 * (t * t * rh)) - (0.000814971 * (t * rh * rh)) + + (0.0000102102 * (t * t * rh * rh)) - (0.000038646 * (t * t * t)) + (0.0000291583 * (rh * rh * rh)) + + (0.00000142721 * (t * t * t * rh)) + (0.000000197483 * (t * rh * rh * rh)) + - (0.0000000218429 * (t * t * t * rh * rh)) + 0.000000000843296 * (t * t * rh * rh * rh)) + - (0.0000000000481975 * (t * t * t * rh * rh * rh))); + } +public: + this(WeatherData weatherData) + { + weatherData.registerObserver(this); + } + + override void update(float temperature, float humidity, float pressure) + { + this.heatIndex = computeHeatIndex(temperature, humidity); + display(); + } + + override void display() + { + writeln("Heat index is ", heatIndex); + } +} \ No newline at end of file diff --git a/observer/weatherdata.d b/observer/weatherdata.d index f097f47..4029c45 100644 --- a/observer/weatherdata.d +++ b/observer/weatherdata.d @@ -16,7 +16,9 @@ public: override void removeObserver(Observer o) { - observers.remove(observers.countUntil(o)); + // Вызовет ошибку в случае отсутствия элемента в массиве после его поиска + // observers = observers.remove(observers.countUntil(o)); + observers = remove!(current => current == o)(observers); } override void notifyObservers()