Dependency Injection (DI)
- is an implementation of the Dependency Inversion Principle (DIP) and Inversion of Control (IoC)
- is a structural design pattern and a refined Strategy Pattern in which one or more dependencies (or services) are injected, or passed by reference, into a dependent object (or client) and are made part of the client’s state. The pattern separates the creation of a client’s dependencies from its own behavior, which allows program designs to be loosely coupled and to follow the inversion of control and single responsibility principles
Code Example
interface IOperation {
void send();
}
class LowLevelModule: IOperation {
@Override
public void send() {
//perform sending operation
}
}
class HighLevelModule {
private IOperation operation;
public HighLevelModule(IOperation operation) {
this.operation = operation;
}
public void Call() {
operation.Send();
}
}
Frameworks
Comparisons to Other Design Patterns
Click here to expand...
Link to originalStrategy Pattern vs Dependency Injection
- Strategy Pattern focuses on intent and encourages you to create an interface with different implementations that obey the same behavioral contract
- Dependency Injection is more about just having an implementation of some behavior and providing it