Upgrade to Pro — share decks privately, control downloads, hide ads and more …

真・なぜ誰もこう書いてくれないDependency Injection入門

Koji Saiki
October 30, 2017

真・なぜ誰もこう書いてくれないDependency Injection入門

Koji Saiki

October 30, 2017
Tweet

More Decks by Koji Saiki

Other Decks in Programming

Transcript

  1. class GasolineCar { private GasolineEngine engine; constructor() { this.engine =

    new GasolineEngine(); } startEngine() { this.engine.start(); } accelEngine() { this.engine.accelerate(); } } ˞ٙࣅίʔυͰ͢
  2. class DieselCar { private DieselEngine engine; constructor() { this.engine =

    new DieselEngine(); } startEngine() { this.engine.start(); } accelEngine() { this.engine.accelerate(); } }
  3. ͳΜͱ͔͍ͨ͠ɾɾɾ &OHJOFҎ֎͸࠶ར༻Ͱ͖ͦ͏ &OHJOF΋࢖͍ํʢTUBSUͱ͔ʣ͸ಉͬ͡Ά͍ class GasolineCar { private GasolineEngine engine; constructor()

    { this.engine = new GasolineEngine(); } startEngine() { this.engine.start(); } accelEngine() { this.engine.accelerate(); } } class DieselCar { private DieselEngine engine; constructor() { this.engine = new DieselEngine(); } startEngine() { this.engine.start(); } accelEngine() { this.engine.accelerate(); } }
  4. interface Engine { void start(); void accelerate(); } class Car

    { private Engine engine; public Car(Engine _engine) { this.engine = _engine; } void startEngine() { this.engine.start(); } void accelEngine() { this.engine.accelerate(); } }
  5. interface Engine { void start(); void accelerate(); } class Car

    { private Engine engine; public Car(Engine _engine) { this.engine = _engine; } void startEngine() { this.engine.start(); } void accelEngine() { this.engine.accelerate(); } } *OUFSGBDFΛ Ҿ਺Ͱ౉ͤ͹εοΩϦʂ
  6. %*લ class Owner { main() { GasolineCar gasolineCar = New

    GasolineCar(); DieselCar dieselCar = New DieselCar(); } } class GasolineCar {} class DieselCar {} )ZCSJE$BSΛ௥Ճ ͠Α͏
  7. %*લ class Owner { main() { GasolineCar gasolineCar = New

    GasolineCar(); DieselCar dieselCar = New DieselCar(); // ௥Ճ HybridCar hybridCar = New HybridCar(); } } class GasolineCar {} class DieselCar {} class HybridCar { private HybridEngine engine; public HybridCar() { this.engine = new HybridEngine(); } startEngine() { this.engine.start(); } accelEngine() { this.engine.accelerate(); } } ͜ͷ͘Β͍௥Ճ
  8. %*ޙ class Owner { main() { GasolineEngine gasolineEngine = new

    GasolineEngine(); Car gasolineCar = new Car(gasolineEngine); DieselEngine dieselEngine = new DieselEngine(); Car dieselCar = new Car(dieselEngine); } } interface Engine { void start(); void accelerate(); } class Car { private Engine engine; public Car(Engine _engine) { this.engine = _engine; } void startEngine() { this.engine.start(); } void accelEngine() { this.engine.accelerate(); } }
  9. %*ޙ class Owner { main() { GasolineEngine gasolineEngine = new

    GasolineEngine(); Car gasolineCar = new Car(gasolineEngine); DieselEngine dieselEngine = new DieselEngine(); Car dieselCar = new Car(dieselEngine); // ௥Ճ HybridEngine hybridEngine = new HybridEngine(); Car hybridCar = new Car(hybridEngine); } } interface Engine { void start(); void accelerate(); } class Car { private Engine engine; public Car(Engine _engine) { this.engine = _engine; } void startEngine() { this.engine.start(); } void accelEngine() { this.engine.accelerate(); } } ͜Ε͚ͩͰ0,
  10. %*લ class Owner { main() { GasolineCar gasolineCar = New

    GasolineCar(); DieselCar dieselCar = New DieselCar(); // ௥Ճ HybridCar hybridCar = New HybridCar(); } } class GasolineCar {} class DieselCar {} class HybridCar { private HybridEngine engine; public HybridCar() { this.engine = new HybridEngine(); } startEngine() { this.engine.start(); } accelEngine() { this.engine.accelerate(); } }
  11. %*લ class Owner { main() { GasolineCar gasolineCar = New

    GasolineCar(); DieselCar dieselCar = New DieselCar(); // ௥Ճ HybridCar hybridCar = New HybridCar(); } } class GasolineCar {} class DieselCar {} class HybridCar { private HybridEngine engine; public HybridCar() { this.engine = new HybridEngine(); } startEngine() { this.engine.start(); } accelEngine() { this.engine.accelerate(); } } ૿͑ͨΠϯλϑΣʔε
  12. %*લ class Owner { main() { GasolineCar gasolineCar = New

    GasolineCar(); DieselCar dieselCar = New DieselCar(); // ௥Ճ HybridCar hybridCar = New HybridCar(); } } class GasolineCar {} class DieselCar {} class HybridCar { private HybridEngine engine; public HybridCar() { this.engine = new HybridEngine(); } startEngine() { this.engine.start(); } accelEngine() { this.engine.accelerate(); } } ςετ͢Δൣғ
  13. %*ޙ class Owner { main() { GasolineEngine gasolineEngine = new

    GasolineEngine(); Car gasolineCar = new Car(gasolineEngine); DieselEngine dieselEngine = new DieselEngine(); Car dieselCar = new Car(dieselEngine); // ௥Ճ HybridEngine hybridEngine = new HybridEngine(); Car hybridCar = new Car(hybridEngine); } } interface Engine { void start(); void accelerate(); } class Car { private Engine engine; public Car(Engine _engine) { this.engine = _engine; } void startEngine() { this.engine.start(); } void accelEngine() { this.engine.accelerate(); } }
  14. %*ޙ class Owner { main() { GasolineEngine gasolineEngine = new

    GasolineEngine(); Car gasolineCar = new Car(gasolineEngine); DieselEngine dieselEngine = new DieselEngine(); Car dieselCar = new Car(dieselEngine); // ௥Ճ HybridEngine hybridEngine = new HybridEngine(); Car hybridCar = new Car(hybridEngine); } } interface Engine { void start(); void accelerate(); } class Car { private Engine engine; public Car(Engine _engine) { this.engine = _engine; } void startEngine() { this.engine.start(); } void accelEngine() { this.engine.accelerate(); } } ૿͑ɾɾɾͯͳ͍ʂ
  15. %*ޙ class Owner { main() { GasolineEngine gasolineEngine = new

    GasolineEngine(); Car gasolineCar = new Car(gasolineEngine); DieselEngine dieselEngine = new DieselEngine(); Car dieselCar = new Car(dieselEngine); // ௥Ճ HybridEngine hybridEngine = new HybridEngine(); Car hybridCar = new Car(hybridEngine); } } interface Engine { void start(); void accelerate(); } class Car { private Engine engine; public Car(Engine _engine) { this.engine = _engine; } void startEngine() { this.engine.start(); } void accelEngine() { this.engine.accelerate(); } } ͜ͷྲྀΕ͚ͩ ςετ͢Ε͹0,
  16. 4QSJOH%*ͷ৔߹ @Component class GasolineEngine { ... } @ComponentScan class MyApp

    { public static void main(String[] args) { ... } } class Car { @Autowired private Engine engine; public Car() { } ... }
  17. 4QSJOH%*ͷ৔߹ @Component class GasolineEngine { ... } @ComponentScan class MyApp

    { public static void main(String[] args) { ... } } class Car { @Autowired private Engine engine; public Car() { } ... }
  18. 4QSJOH%*ͷ৔߹ @Component class GasolineEngine { ... } @ComponentScan class MyApp

    { public static void main(String[] args) { ... } } class Car { @Autowired private Engine engine; public Car() { } ... } Ξϊςʔγϣϯ͚ͭΔ ͚ͩͰOFXͯ͠ϑΟʔϧυʹηο τͯ͘͠ΕΔ ʹ͍͢͝
  19. 4QSJOH%*ͷ৔߹ @Component(“engine”) class GasolineEngine implements Engine { ... } class

    Car { @Autowired private Engine engine; public Car() { } ... } @Component(“old_engine”) class DieselEngine implements Engine { ... }
  20. 4QSJOH%*ͷ৔߹ @Component(“engine”) class GasolineEngine implements Engine { ... } class

    Car { @Autowired private Engine engine; public Car() { } ... } @Component(“old_engine”) class DieselEngine implements Engine { ... }