Codementor Events

Dependency Injection Explained in Plain English

Published Aug 22, 2017Last updated Feb 17, 2018

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 consumption. This prevents the client from modifying when the underlying service 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 like gibberish, but I’m about to explain what this all means in plain and simple English.

Dependency Injection in Plain and Simple English

Dependency Injection can exist between two objects, for instance, a flashlight and a battery. The flashlight needs the battery to function. However, any changes made to the battery, such as switching it with another brand/set of batteries, does not mean the dependent object (flashlight) also needs to be changed.

A flashlight needs power from a battery to produce light. A flashlight that only works with a particular brand of battery is badly designed.

If a flashlight stops working when the battery is changed, it means the approach is bad, because what a flashlight actually needs is power from a battery and not necessarily from a particular type of battery.

Creating a flashlight whose battery can easily be swapped would be a better approach. This way, changing the battery from one brand to another shouldn't impact the flashlight's ability to draw power from the new battery.

Electronics, such as televisions and refrigerators, need power to function properly. For a television, a power supply is needed for it to produce images, videos, and sound. A refrigerator needs a power supply to preserve food.

Changing the power supply source from the main source to a generator should still make these electronics work because all they need is power — not necessarily power from a particular predefined source.

A form of dependency injection would be to create devices that are independent of the type of power source.

Another analogy for dependency injection is between a vehicle and a tire. A vehicle needs tires to move and tires provide vehicles enough balance for movement. Therefore, vehicles depend on tires in general for their movement, not a specific tire brand.

The vehicle only "cares" about its tires when its about to move. Changing the tire brand doesn't mean the entire vehicle has to be remanufactured.

Conclusion

Dependency injection is the art of creating service/client relationships that work well together by injecting services that are easily swapped with minimal time and effort.

Although the components are mutually dependent, replacing one won't harm or render the other useless.

In the next part, I’ll be explaining with CODE examples because I know you’ve been itching to get your hands dirty using this concept practically in your codebase.

Stay tuned and try figuring out this simple example in the image below! :smile:

example

Please let me know if you have any questions or observations in the comments section below.

Useful Resources


This post is originally published by the author here. This version has been edited for clarity and may appear different from the original post.

Discover and read more posts from Olotin Temitope
get started
post commentsBe the first to share your opinion
Nam Lu
7 years ago

Hi Olotin, maybe some coding examples might help. What are some examples of DI in the real-world?

Olotin Temitope
7 years ago

Thanks, Nam Lu. My next post will be on the real world examples of how to use Dependency Injection in software design.

Show more replies