The main advantage of dependecy injection is isolation from changes to input objects. So in your case, one variable is obvious - that is size. You must add fees to the main board so that the main board no longer needs to know or worry about the size. Also, if your application does not have to support 3 different behaviors between different board types, I would suggest using one abstract definition for a board type interface.
public class MainBoard { private IBoardType bt1; private IBoardType bt2; private IBoardType bt3; public MainBoard(IBoardType bt1, IBoardType bt2, IBoardType bt3) { this.bt1 = bt1; this.bt2 = bt2; this.bt3 = bt3; } }
Responsibility for what the injection does (injection frame or assembly code) is responsible for ensuring that these boards get the correct size. This can be done in several ways: one example is the main board, and nested boards - they all get their size from one external source. Perhaps your application, in this case, is the size of the input boards relative to the main board.
So you can have external logic, for example:
public class BoardAssembler { public static MainBoard assembleBoard(Size size) { Size innerBoardSize = deriveSizeOfInternalBoards(size); return new MainBoard(new BoardType1(innerBoardSize), new BoardType2(innerBoardSize), new BoardType3(innerBoardSize)); } }
Essentially, you follow the inverse of the build logic wherever MainBoard is built. Start there and retrieve everything in the factory or some kind of evil singleton factory or static method. Ask yourself: "Where is MainBoard created?" Also ask: "What components and parameters are needed?" Once you have moved all the logic of creating an instance to a factory, it may be easier to maintain Mainboard and all its dependencies.
Cliff
source share