I have an OOP design question (I am using java but the question is more general). I am programming home automation simulator and I have some trouble designing it. There's a class for each appliance, like Television or Heating with corresponding methods like volumeUp(), ... I would like decouple classes for appliances and sensors which are generating events and executing actions on them. I could think of this: I'll implement observer pattern to propagate events from sensors to alarm classes. Alarm class looks like this:
class Alarm implements Observer {
Action action;
public update(...) { action.do() }
}
Now, since I want to let the user to be able to arbitrarily couple sensors and actions they trigger in user interface I've thought of creating separate class for each action on each appliance
class ActionTVVolumeUp implements Action {
Television tv;
ActionTVVolumeUp(Television tv) { this.tv = tv; }
public void do() { tv.volumeUp() }
}
and then when user specifies sensor (MotionSensor,LightSensor,...) in user interface and action to trigger I'll create Alarm instance with desired action observing desired sensor. The problem with this is that I am going to need many and many action classes (possibly reducing them a bit by using interfaces like VolumeControllable which appliances would implement).
Is there something I am missing and could this be done simpler/without need to use so many Action classes? Thank you.