private final CoalPowerPlant coalPowerPlant; private final ElementarySchool elementarySchool; public MyCity() { this.waterTower = new WaterTower(); this.coalPowerPlant = new CoalPowerPlant(); this.elementarySchool = new ElementarySchool(); } @Override public Water water() { return waterTower.water(); } @Override public Electricity electricity() { return coalPowerPlant.electricity(); } @Override public Education education() { return elementarySchool.education(); } }
private final PowerPlant powerPlant; private final SchoolSystem schoolSystem; public MyCity() { this.waterSource = new WaterTower(); this.powerPlant = new SolarPowerPlant(); this.schoolSystem = new ElementarySchool(); } @Override public Water water() { return waterSource.water(); } @Override public Electricity electricity() { return powerPlant.electricity(); } @Override public Education education() { return schoolSystem.education(); } }
Objects instantiated without reflection • Flexible model (overrides) Cons • Graph composition happens at runtime • Generated code can be hard to read • Does not work with Proguard Dagger 1