IoC, DI, and a mess...
Hmm, I thought I posted this earlier, but Google says otherwise. If
this is a repeat, please respond to the *other* post.
So, I have a RobotFactory, which creates and Robot, all its
components, and then wires the components to the robot. This is my
attempt at using IoC and dependency injection for this.
The wiring is done in one method (shown below). The factory can be
configured once, and then used several times to create new robots that
are the same. The application can have many different RobotFactory
instances.
The problem I have, is that this wiring looks like a mess. I'm trying
to think of ways that could simplify this construct will still giving
me the IoC.
void configureRobot(Robot robot) {
robot.setThrottle(createThrottle());
robot.getThrottle().setRobot(robot);
robot.getHeat().setCoolMultiplier(
chooseDoubleFor("heatsinks", 0.75, 1.00, 1.125, 1.25, 1.33,
1.50));
robot.setArmor(createArmor());
robot.getArmor().setRobot(robot);
robot.setMineLayer(createMineLayer());
robot.getMineLayer().setRobot(robot);
robot.setRadar(createRadar());
robot.setShield(createShield());
robot.getShield().setRobot(robot);
robot.setSonar(createSonar());
robot.getSonar().setRobot(robot);
robot.setTransceiver(createTransceiver(robot));
robot.setTransponder(createTransponder());
robot.setTurret(createTurret());
robot.getTurret().getHeading().setRelation(robot.getHeading());
robot.getTurret().setKeepshift(false);
robot.getTurret().getHeading().setAngle(robot.getHeading().getAngle());
robot.getTurret().setMissileLauncher(createMissileLauncher(robot));
robot.getMineLayer().setArena(robot.getEntrant().getGame().getRound().getArena());
robot.getComputer().setCommQueue(new CommunicationsQueue(
robot.getComputer().getCommQueueMemoryRegion(),
robot.getComputer().getRegisters().getCommunicationQueueHead(),
robot.getComputer().getRegisters().getCommunicationQueueTail()));
robot.getTransceiver().setCommQueue(robot.getComputer().getCommQueue());
robot.getComputer().getCommQueue().setComputerErrorHandler(robot.getComputer().getErrorHandler());
robot.getComputer().getMemory().setErrorHandler(robot.getComputer().getErrorHandler());
robot.getTurret().setScanner(createScanner());
robot.getTurret().getScanner().setRobot(robot);
}
So, as you can see. A big mess. The object graph is a bit of a mess,
but once you've got it wired, it "just works".
Any suggestions for a different approach? Maybe I should create a
context class that knows about all the objects independently of each
other. It doesn't know (or care) how to create them, it gets them
through DI. All it knows about is how to wire them together. That lets
me completely decouple the wiring from the creation. This really
seems like a meta-problem that should be easily solvable.