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?
1 Answer 1
I think your initial analysis into classes and interfaces is not quite right. I go by the mantra "if you can kick it, then it should be a class instance" (apologies to the purists out there). You can definitely kick a door, so there should be a class Door which will have methods for open(), close() and lock(), plus listeners for onOpen(), onClose() and onAlarm(). A constructor like Door(int id) sounds about right, allowing you to give the door a primary key.
The manager class should maintain a list of doors, and do the work required to:
- route incoming commands from the socket interface to the appropriate door instance
- send outgoing events from doors to the socket interface.
The Door class should not know that the socket implementation exists. Your DoorConnectionIF interface is likely superfluous.
But it is good to see someone understanding why God invented listeners. Many experienced programmers misunderstand what listeners are designed for, and end up with tangled circular references between classes.
private
and if you instantiate them at their declaration, you can also make themfinal
. \$\endgroup\$