DEV Community

Cover image for Dependency Injection
Jesse M. Holmes
Jesse M. Holmes

Posted on

Dependency Injection

Morgan's Flowers

Morgan owns a flower shop, and it is important that they take delivery of new stock each day so that they can continue to supply their customers.

Without Dependency Injection

Ashley pulls into the loading dock in a truck filled with fresh flowers. Morgan accepts the delivery and says, "Thanks, Ashley! I will see you tomorrow!"

Morgan has decided to include Ashley as a valuable part of her system. What happens if Ashley doesn't deliver the flowers tomorrow? What if Ashley changes jobs, gets fired, or just isn't as reliable as Morgan expected? Because Ashley is built into Morgan's business system, Morgan is stuck. To make a change for their business, they are going to have to rebuild the entire supply chain again, interview other drivers, change delivery methods, adjust schedules, etc. Do you see the problem here?

With Dependency Injection

Ashley pulls into the loading dock in a truck filled with fresh flowers. Morgan accepts the delivery and says, "Thanks!" Morgan doesn't know Ashley, and they don't care if Ashley is driving the truck tomorrow. Morgan is only interested in taking delivery of the flowers, not who delivers them. In fact, if Morgan is dissatisfied with the delivery service, they can order the same flowers from a new delivery service.

Did you find this analogy helpful?

I'd like to write a series of posts like this one that I can refer back to when teaching. There is no code included in this example. Is that an issue? Is there a topic you'd like to tackle in this manner? Let me know!

Latest comments (2)

Collapse
 
tyson1202parker profile image
Ty

Actually, I have more questions, is there a way to get in touch with you?

Collapse
 
tyson1202parker profile image
Ty

I enjoy this example, for it is something relatable, and easy for me to understand. Thank you.