vendredi 27 février 2015

Observer Pattern For Different Observables

I was wondering what the appropriate way of dealing with Observables that may contain different data. To use the weather data analogy:


suppose I have different weather stations that record data. Let's say humidity, temperature and pressure. One station might only have the capability of recording temperature, while others all three etc..


What I am trying to do is the following:



  • An observer specifies what data they want to record. Say, only temperature.

  • An observable specifies what data they have available.

  • If both the observer and observable match up, the data is processed by the observable.


Here are a few things: There are a lot more parameters than 3. Something like 30, and this can grow. I thought of implementing getTemperature(), getPressure(), getHumidity(), etc.. in my base observable and overriding them in the relevant classes. Otherwise it returns nothing. I also created a Flags anonymous struct that is specified for both the Observable and Observer, and only when they match data is recorded.


I wanted to know the following: Is this the best design? Should the responsibility of matching Flags be on the Observer? Is there a more elegant solution?


I'm not necessarily looking for code handouts. Just thoughts on a good implementation.


Thanks!


Aucun commentaire:

Enregistrer un commentaire