samedi 19 mars 2016

c++ observer pattern: adding another dimension

I'm trying to implement this pattern on a "smart building" system design (using STL library). Various "sensors" placed in rooms, floors etc, dispatch signals that are handled by "controllers" (also placed in different rooms, floors etc.). The problem I'm facing is that the controller's subscription to an event isn't just event based, it is also location based.

For example, controller A can subscribe to a fire signal from room #1 in floor #4 and to a motion signal in floor #5. A floor-based subscription means that controller A will get an motion event about every room in the floor he's subscribed to (assuming the appropriate sensor is placed there). There's also a building-wide subscription for that matter.

The topology of the system is read from a configuration file at start up, so I don't want to map the whole building, just the relevant places that contain sensors and controllers.

What I've managed to think of :

Option 1: MonitoredArea class that contains the name of the area (Building1, Floor 2, Room 3) and a vector where the vector's index is an enumerated event type each member of the vector contains a list of controllers that are subscribed to this event. The class will also contain a pointer to a parent MonitoredArea, in the case it is a room in a floor, or a floor in a building.

A Sensor class will dispatch an Event to a center hub along with the sensor's name. The hub will run it through his sensor-name-to-location map, acquire the matching MonitoredArea and will alert all the controllers in the vector.

Cons:

  • Coupling of the location to the controller
  • Events are enumerated and are hard coded in the MonitoredArea class, adding future events is difficult. enter image description here

Option 2: Keeping all the subscriptions in the Controller class.

Cons:

  • Very inefficient. Every event will make the control center to iterate through all the controller and find out which are subscribed to this particular event.

Option 3: Event based functionality. Event class (ie. FireEvent) will contain all the locations it can happen in (according to the sensor's setup) and for every location, a list of the controllers that are subscribed to it.

enter image description here

Cons:

  • A map of maps
  • Strong data duplication
  • No way to alert floor-based subscriptions about events in the various rooms.

As you can see, I'm not happy with any of the mentioned solutions. I'm sure I've reached the over-thinking stage and would be happy for a feedback or alternative suggestions as to how I approach this. Thanks.

Aucun commentaire:

Enregistrer un commentaire