Dependency Inversion Principle
Definition: Instead of lowel level modules defining an interface that higher level modules depend on, higher level modules define an interface that lower level modules implement.
Example: portable chargeable devices, the copy program (read and write)
Inversion of Control
- Interface inversion
The user of the class defines the interface
Interface should have more than 1 implementation
- Flow inversion
Procedural VS Event driven
- Creation / binding inversion
Creating objects outside of the class they are being used in.
A type of IOC where we move the creation and binding of a dependency outside of the class that depends on it.
Example: packing a lunch VS lunch is provided.
- Constructor Injection
Pass dependency into dependent class via constructor.
- Setter Injection
Create a setter on the dependent class and use the setter to set the dependency.
- Interface Injection
Dependent class implements an interface. Injector uses the interface to set the dependency.