↓ Ir para o conteúdo principal

← todas as notas

📎 Webclip

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

The page explains dependency injection as giving an object the things it needs from outside instead of making it create and manage them itself. One answer uses the idea of asking for a drink at lunch instead of taking whatever is in the refrigerator, and another uses an Employee that receives an Address, with the benefit that tests can supply mock objects.

Reading notes
#

  • One answer frames it as stating a need and letting someone else provide the dependency.
  • Another answer shows an Employee class that receives an Address through the constructor or a setter.
  • That same example says direct construction creates a dependency chain that becomes awkward for unit testing.
  • The answer also says a Department dependency can be shared by many objects, which is easier when it is injected from outside.
  • The framework is described as the thing that sets the correct objects for a given scenario, often from external configuration.
  • Another answer says the object is configured by higher-level logic and then calls components it did not know about beforehand.
  • One answer uses a Nintendo example to show that the user can operate the device without knowing how it is assembled inside.