views:

655

answers:

11

Hello,

I have read a lot of articles on Dependency Injection as well as watched a lot of videos, but I still can't get my head around it. Does anyone have a good analogy to explain it?

I watched the first part of the Autumn of Agile screencast and still was a little confused.

+37  A: 

Analogy? I'll give it a whack... Your CD Player stereo is useless without a CD with music on it... (It's dependent on the CD). If they built CD Players with the CD already in it, it would get boring very quickly...

So they build them so you can "inject" the CD, (on which it is dependent) into the player. That way you can inject a different one each time, and get "different" behavior (music) dependent on which one you inject.

Charles Bretana
Nice. To extend it, the player knows about CD's in general - how to read them, etc, (the interface) but it doesn't know anything about a specific album (implementation).
ctacke
Great analogy! I'll have to remember this one.
chills42
Superb. It goes in my toolbox.
Will
Yes, that makes it much better to understand.
Xaisoft
Real nice one mate. Great addition from ctacke as well.
willcodejavaforfood
Wow ! this is brilliant ! Thanks for that one !
tsimbalar
+1  A: 

Maybe focus on the "injection" part? When I see that term, I think of syringes. The process of pushing the dependencies of a component to the component can be thought of as injecting into the component.

Just like with the body, when there is something that it needs in the way of medicine (a component that it needs) you can inject it into the body.

casperOne
+5  A: 

The best analogy I can think of is that of hiring a mechanic.

Without dependency injection, you hire a mechanic and the mechanic brings his own tools. He may have lousy tools, he may have great tools, he may be using a pipe wrench when he should be using a socket. You don't know, and may not care, so long as he gets the work done.

With dependency injection, you hire a mechanic and you provide him with the tools that you want him to do his work with. You get to choose what you consider to be the best or most appropriate tools for the work you are hiring him to do.

Stever B
+1  A: 

Your project manager asks you to write an app.

You could just write some code based on your career experience so far, but it's unlikely to be what your PM wants.

Better would be if your PM dependency injected you with say a spec for the app. Now your code is going to be related to the spec he gives you.

Better if you were told where the source repository was.

Better if you were told what the tech platform was.

Better if you were told when this needed to be done by.

Etc..

annakata
+1  A: 

In their 2003 JavaPolis presentation (slides), Jon Tirsén & Aslak Hellesøy had an amusing analogy with a Girl object that needs a Boy to kiss. I seem to remember that the BoyFactory is sometimes known as a 'nightclub', but that's not in the slides.

Peter Hilton
With (of course) no intent to imply the obvious (much better) analogy which relies on pregnancy "dependencies" and how they are "Injected"
Charles Bretana
+1  A: 

Another analogy: let us say you are a developer and whenever you like you order computer science books from the market directly - you know the sellers and their prices. In fact your company might have a preferred seller and you contact them directly. All this works fine but may be a new seller is now offering better prices and your company wants to change the 'preferred' seller.

At this point you have to make the following changes - update the contact details (and other stuff) so as to use the new seller. You still place the order directly.

Now consider we introduce a new step in between, there is a 'library' officer in the company and you have to go through him to get the books. While there is a new dependency, you are now immune to any changes to the seller: either the seller changes mode of payment or the seller himself is changed, you now simply put an order to the librarian and he gets the books for you.

Sesh
+1  A: 

Think of it as a realisation of the "Inversion of Control" pattern. I guess, your problem is, you are so used to it, you don't realize it's that simple.

Let's start at the beginning.

In the early days programs followed a given path through the code. The order of the called functions was given by the programmer.

In interactive programs, e.g. mostly ANY program, you can not say, which function is called at what time. Just look at a GUI or website. You can not say, at what time what button or link is clicked. So the "control" of what's happening is no longer at the program, it's at an outer source. The "control" has been inverted. The function is no longer "acting" it is instead "listening". Think of the hollywood principle: "Don't call us, we call you". A listener is a good example for a realisation of this pattern.

IoC is realized by functions or "methods" in the "object oriented world" of today.

"Dependency Injection" now means the same, but not for "methods", which do something, but for "objects", which hold data.

The data is no longer part of the object holding it. It is "injected" into the object at runtime. To stay in hollywood, think of a film star, playing golf to talk about the business, but to keep in shape, she hungers herself down, minimizing her muscle weight and therefore she is only able to carry one club at a time.

So, on the golf course her game would heavily depend on the one club, she is carrying.

Lucky for her, there are caddies, carrying a whole lot of clubs at one time, and also having the knowledge what club to use at what time. Now she is independent of her limited possibility to carry golf clubs. "Don't think about a concrete club to wear, we know them all and give you the right one at the right time".

The film star is the object and the golf clubs are the members of the object. That's dependency injection.

bitschnau
+1  A: 

From Head First Design Patterns:

Remember, code should be closed (to change) like the lotus flower in the evening, yet open (to extension) like the lotus flower in the morning

A DI-enabled object can be configured by injecting behaviors defined in other classes. The original object structure doesn't have change in order to create many variations. The injection can be made explicit by having a class request other worker-classes in its constructor, or it can be less obvious when using monkeypatching in dynamic languages like Python.

Using an analogy of a Person class, you can take a basic human framework, pass it a set of organs, and watch it evolve. The Person doesn't directly know how the organs work, but their behaviors confirm to an expected interface and influence the owner's physical and mental manifestation.

Mike Griffith
+1  A: 

A magician's sleight of hand! What you may think you see may be secretly manipulated or replaced.

Kris Kumler
+1  A: 

Life is full of dependency injection analogies:

  • printer - cartridge
  • digital device - battery
  • letter - stamp
  • musician - instrument
  • bus - driver
  • sickness - pill
Przemek
+1  A: 

The essence of Inversion of Control (of which Dependency Injection is an implementation) is the separation of the use of an object from the management thereof.

The analogy/example I use is an engine. An engine requires fuel to run, i.e. it is depdendent on fuel. However, the engine cannot be responsible for the fuel it needs. It just 'asks' for fuel, and it is provided (typically by a fuel pump in a car).

The analogy starts breaking down when you look too deep, in that an engine doesn't ask for fuel, it is given it by some kind of management element, like an ECU. One might be able to compare the ECU to a container but I'm not certain how valid this is.

Joshua Lewis