notifyObservers first variant

This commit is contained in:
Alexander Zhirov 2022-11-11 10:22:38 +03:00
parent 8170c93255
commit 850cb17d02
3 changed files with 45 additions and 2 deletions

View File

@ -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);
}

View File

@ -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);
}
}

View File

@ -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()