Dependency Injection
Dependency injection moves the creation and binding of dependencies to an external container; components receive dependencies via constructor/property/interface, decoupling callers.
Purpose
Reduce module coupling and improve testability and configurability — the implementation of DIP in SOLID.
Mechanism
Constructor injection: the constructor receives dependencies. Property injection: setters assign dependencies. Interface injection: implementing an interface to receive them. A container (Spring/DI frameworks) injects per configuration. Inversion of Control (IoC) is the core idea.
Use cases
- Decoupled design
- Unit testing
- Framework design