Dependency Injection Explained in Plain English
What is Dependency Injection?
Dependency Injection is a software design concept that allows a service to be used/injected in a way that is completely independent of any client consuming it. This prevents the client from changing when the underlying service being consumed changes.
Dependency injection separates the creation of a client’s dependencies from the client’s behavior, which allows program designs to be loosely coupled. I know that might sound gibberish but trust me, I’m about to explain in plain, simple English.
Now, Dependency Injection can exist between two objects, take for instance a torch and a battery. The torch needs the service of the battery to function. However, any changes made to the battery such as switching it with another brand/set of battery does not mean the dependent object (torch) will have to change.
It is a known fact that a torch needs power from a battery to produce light. Creating a torch that can only work with a particular brand of battery would be a bad design. When the battery is changed, the torch stops working. This is a bad approach since what a torch actually needs is power from a battery and not necessarily a particular type of battery. Creating a torch whose battery can easily be swapped would be a better approach. This way, changing the battery from one brand to another should make the torch utilise the power source from the battery.
Electronics such as televisions and refrigerators need power to function properly. As for a television, power supply is needed for it to produce images, videos and sound for entertainment. Also, a refrigerator needs power supply to preserve food. Changing the power supply source from the main source to a generator should still make these electronics work. Since all they need is power and not power from a particular predefined source. A form of Dependency Injection would be creating devices that are independent of the kind of power source.
Another analogy of Dependency Injection would be between a vehicle and a tyre. A vehicle needs tyres for its movement and tyres provide vehicles enough balance for movement. Therefore, vehicles depend on tyres for their movement but not a specific brand of tyres. When a vehicle is set to move, it only bothers about its tyres. Changing the brand of tyres does not mean the entire vehicle has to be remanufactured.
In summary, Dependency Injection involves the art of creating service/client relationships that can work fine together by injecting services in a way that can easily be swapped with alternatives in the least amount of time and with minimal efforts. Although, both are dependent on each other, the replacement of one will not harm or render the other useless.
In the next part, I’ll be explaining with CODE examples. Yes, I know you’ve been itching to get your hands dirty with using this concept practically in your codebase. Stay tuned and try figuring out this simple example in the image below!
Please let me know if you have any questions or observations in the comments section below.