How to explain dependency injection to a 5-year-old?

user198313 picture user198313 · Oct 28, 2009 · Viewed 96.4k times · Source

What is a good way to explain dependency injection?

I found several tutorials on Google, but none of them that would assume the reader is just a Java beginner. How would you explain this to a novice?

Answer

John Munsch picture John Munsch · Oct 28, 2009

I give you dependency injection for five-year-olds.

When you go and get things out of the refrigerator for yourself, you can cause problems. You might leave the door open, you might get something Mommy or Daddy doesn't want you to have. You might even be looking for something we don't even have or which has expired.

What you should be doing is stating a need, "I need something to drink with lunch," and then we will make sure you have something when you sit down to eat.