Dependency injection
Dependency injection is a design principle that states:
-
High-level modules SHOULD NOT depend on low-level modules. Both SHOULD instead depend on abstractions.
-
Abstractions SHOULD NOT depend on details. Details – concrete implementations – SHOULD depend on abstractions.
Thus, both the high-level module and its low-level dependency MUST depend on the same abstraction. This design principle inverts the way people naturally think about [object-oriented programming] as a means of composing complex programs from lots of smaller, simpler components.
Dependency injection is one possible solution to achieving Inversion of control (IoC).
Numerous design patterns can be used to implement dependency injection, notably dependency injection containers (DICs) and service locators.