Proponents of dependency injection try to design classes so they can either work autonomously or get all services they rely on handed to them through their constructor. But even without dependency injection, the situation often arises where certain classes need to interact with a lot of other objects.
In these cases, you often end up with very complicated constructors and a lot of duplicate code:
public class RadarBuildingRenderer {
public RadarBuildingRenderer(
ISceneGraph sceneGraph,
IContentManager contentManager,
IAudioManager audioManager,
RadarBuilding building
) {
this.sceneGraph = sceneGraph;
this.contentManager = contentManager;
this.audioManager = audioManager;
this.building = building;
}
private ISceneGraph sceneGraph;
private IContentManager contentManager;
private IAudioManager audioManager;
private RadarBuilding building;
}
Above class takes care of rendering the visual and audible representations
of a RadarBuilding
in a computer game. As you can imagine,
the same references will be required by other buildings, think
TankFactoryBuilding
, CommandCenterBuilding
and so
on – all duplicating the fields, their assignment and the complex constructor.