The point is to reduce how much Manager
knows. Also, SystemMoniter
should only know its things. it shouldn't need to know about Leds, Speakers, or the Screen. And actually, neither should the Manager.
class Manager{
// maybe Manager should only know a List<CallbackSchedulerInterface>,
// and not know about SystemMangerSystemMoniter directly?
SystemMonitor systemMonitor;
public void scheduleCallback(){
systemMonitor.scheduleCallback();
}
}
There should be an alarm listener interface
public interface SystemAlarmListener {
void onSystemAlarmGoesOff();
}
And then the SystemMonitor only knows about the interface
private class AlarmListeners {
private Set<SystemAlarmListener> alarmListeners;
public onSystemAlarmGoesOff() {
for(SystemAlarmListener listener : alarmListeners)
listener.onSystemAlarmGoesOff();
}
}
class SystemMoniter {
AlarmListeners alarms;
public void scheduleCallback() {
if(this.temperature>100)
alarms. onSystemAlarmGoesOff();
}
}
And all your other things implement that interface
class LED implements SystemAlarmListener {
public onSystemAlarmGoesOff() {
led.turnRed();
}
}
class Speaker implements SystemAlarmListener {
public onSystemAlarmGoesOff() {
speaker.play("alarm.mp3");
}
}
class Screen implements SystemAlarmListener {
public onSystemAlarmGoesOff() {
screen.play("alarm.mp4");
}
}
Note that each class is only told, never asked. Each class manages it's single responsibility, and responsibility is not shared. And, bonus: Note that since everything depends on the tiny SystemAlarmListener
, now all of your classes can compile in parallel, which makes compilation MUCH faster (depending on your build system).