This repository has been archived on 2022-11-20. You can view files and clone it, but cannot push or open issues or pull requests.
patterns/observer/request/weatherdata.d

61 lines
1.3 KiB
D
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

module observer.request.weatherdata;
import observer.request.subject;
import observer.request.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 = observers.remove(observers.countUntil(o));
observers = remove!(current => current == o)(observers);
}
override void notifyObservers()
{
foreach (Observer o; observers)
{
o.update();
}
}
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;
}
}