1

Possible Duplicate:
Understanding the need for a DI framework

I have read several ariticles and talked to several professionals regarding DI. What I Dont understand is the point that how to DI decouples the objects. The following article also describes a scenario but I dont understand how does Spring help in decouplling PayrollApplication? Decoupling is done with polymorphism rather than DI. DI definelty serves some other purpose...

http://www.developer.com/java/dependency-injection-in-existing-java-code.html

Hints and refrences will do. Thanks

Community
  • 1
  • 1
Pinser
  • 1,898
  • 3
  • 29
  • 43
  • My argument is specifically that I dont see how DI is responsible for decoupling. The objects that are needed before are needed now as well! – Pinser Jan 23 '13 at 17:36
  • The decoupling is performed in the design, DI has nothing to do with that. You could use DI on objects that reference concrete implementations and there wouldn't be any decoupling at all. You decouple your different beans when you make depend on abstract classes or interfaces, which allows you to switch different impls easily. a DI container just saves you from all the hassle needed to locate the different instances of each bean and inject them to the required beans. – Alonso Dominguez Jan 23 '13 at 18:50
  • 1
    The article you reference is a really bad example of explaining Dependency Injection. You don't see the difference between Dependency Injection and Service Location and neither Inversion of Control nor the Dependency Inversion Principle is explained. – Rookian Jan 23 '13 at 20:16

2 Answers2

1

Dependency Injection decouples the objects because you are not instantiating the concrete classes directly, Spring is doing it for you. The alternative to using DI is that you would typically create a reference to an interface, but you would still have to instantiate the object directly. You still have the concreate class referenced in your code.

Edit: Forgot to mention you can use the Factory pattern to take the implementation out of your code, but then you are simply moving it into the Factory. Spring is like a gigantic, generic Factory of all beans.

CodeChimp
  • 8,016
  • 5
  • 41
  • 79
1

Decoupling is not an all-or-nothing concept. In this article using polymorphism only ("Design 2") decouples the objects to a certain degree, but the PayrollApplication class still has to know about the Employee implementations when PayrollApplication is compiled.

The Employee implementations are the dependencies of PayrollApplication, and with dependency injection you can achieve a higher level of decoupling: you can compile PayrollApplication so that it only references the Employee interface, and supply the Employee implementations (possibly written by another team/organization) only later.

Also check out the Wikipedia about dependency injection:

The primary purpose of the dependency injection pattern is to allow selection among multiple implementations of a given dependency interface at runtime, or via configuration files, instead of at compile time. The pattern is particularly useful for providing stub test implementations of complex components when testing, but is often used for locating plugin components, or locating and initializing software services.

lbalazscs
  • 17,474
  • 7
  • 42
  • 50