I would much appreciate your advice on this design I am going to implement. I am relatively new to object programming and I am not so sure about the delegate pattern.
I need to provide the implementation of a class that implements several interfaces of different nature. As the class would be huge I am thinking on using the delegate pattern to implement the code for each interface in a separate class. Simplifying the code a bit it goes like this:
The interfaces I have:
public interface DoorControlIF {
public void openDoor(int id);
//some more door operations
}
public interface DoorStatusListenerIF {
public void onDoorOpened(int id);
}
public interface DoorAlarmListenerIF {
public void onDoorAlarm(int id, String alarmType);
}
public interface DoorConnectionIF {
public void connectToDoor(int id);
public void disconnectDoor(int id);
}
public interface DoorNotifierIF {
public void addDoorStatusListener(DoorStatusListenerIF listener);
public void addDoorAlarmListener(DoorAlarmListenerIF listener);
}
Then the Manager
class would be like:
public class DoorManager implements DoorControlIF, DoorConnectionIF, DoorNotifierIF{
SocketController socketController;
DoorController doorController;
DoorNotifier doorNotifier;
public void init() {
socketController = new SocketController();
doorController = new DoorController();
doorController.setSocketController(socketController);
doorNotifier = new DoorNotifier();
doorNotifier.setSocketController(socketController);
}
public void connectToDoor(int id) { socketController.open(id); }
public void disconnectDoor(int id) { socketController.disconnect(id); }
public void openDoor(int id) { doorController.openDoor(id); }
public void addDoorStatusListener(DoorStatusListenerIF listener) { doorNotifier.addDoorStatusListener(listener); }
public void addAlarmStatusListener(DoorAlarmListenerIF listener) { doorNotifier.addDoorAlarmListener(listener); }
Then we would have all the delegate classes implement each one of them a specific interface:
public SocketController implements DoorConnectionIF{}
public DoorController implements DoorControlIerF{}
public DoorNotifier implements DoorNotifierIF{}
Particularly I am not so sure if it is right or not having each delegate class implementing again the same interface as the the Manager
class. Then there's this forwarding of listeners that are added to the Manager
but inside my class I add to my specific notifier. How about passing the SocketController
to each of the delegates?
private
and if you instantiate them at their declaration, you can also make themfinal
. \$\endgroup\$