Reducing class dependencies with the dependency injection pattern
This pattern separates the initialization of the class (that acts as a service) from the client (that uses the service).
Motivation
The dependency injection pattern is widely used where there is a need to separate the implementation of a particular object (service) from the target object (client) that uses its exposed services, methods, and the like. Services are available when a client instance is to be created. The pattern allows you to eliminate any hardcoded dependencies. These services are instantiated outside of the client creation process. This means that the two are loosely connected, and SOLID principles can be enforced. There are three ways that dependency injection can be implemented:
- Constructor dependency injection: Intended services are made available to the client through the initialization of the constructor.
- Injection method: The client exposes the method normally through an interface...