Dependency injection
Dependency injection is a design pattern that handles dependencies and resolves them. An instance of the dependencies will be passed to the dependent in order to use it. If a client module or class is dependent on a service, it needs to create an instance of the service before using it. We can inject or pass the instance of the service to the client using a dependency injection pattern, rather than a client module building the service.
Applying dependency injection enables us to create a client that does not have any knowledge of the service to be built and of the actual service it is consuming. The client will only have knowledge about the interface of the service as it needs to know how to use the service.
Why dependency injection?
Consider that we are creating a Mobile
class, and it has dependency on a camera
and internet
connectivity.

The code snippet of a Mobile class
In the preceding code snippet, you can see that the instances of Camera
and Internet
are created in...