I would like to ask how I should correctly implement observer pattern when I need to achieve something like this:
WeatherStation[temperature, humidity ...]
and I need to be able to "observe" each attribute independently. So when temperature changes only temperature observers will be notified, when humidity changes only humidity subscribers will be notified.
My idea was that I would create some classes like ObservableTemperature and interface TemperatureObserver but I this way I would have to create two "classes" for each attribute.
Second option is to create only two interfaces for each attribute (something like TemperatureSource, TemperatureObserver ...) and then implement the xxxSource interface in WeatherStation class but this way it is not reusable and I would need to have in WeatherStation class a lot of arrays (same number as "observable" attributes) keeping track of observers.
Is there any better option?
EDITED: Also it can also happen that I would have something like Display class which would subscribe to multiple attributes(not all) and still need to distinguish which one of them was updated.
Aucun commentaire:
Enregistrer un commentaire