It's not a secret anymore!*
The moose likes OO, Patterns, UML and Refactoring and the fly likes Have I achieved dependency Injection? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Have I achieved dependency Injection?" Watch "Have I achieved dependency Injection?" New topic
Author

Have I achieved dependency Injection?

Hendra Kurniawan
Ranch Hand

Joined: Jan 31, 2011
Posts: 239
I'm still confused with the concept DI. In a nutshell, I perceive that DI is basically only aims to loosen coupling. Some help here? thanks
Ran Pleasant
Ranch Hand

Joined: Jan 16, 2003
Posts: 75
Hendra Kurniawan wrote:I'm still confused with the concept DI. In a nutshell, I perceive that DI is basically only aims to loosen coupling. Some help here? thanks


It's not the only goal, but it is the major goal. Another major benefit of DI is being allowed to write code without having to worry about where other objects are coming from. For exmaple, you can write a service that uses a couple of DAOs without having to think about creating or getting the DAOs. The field type for each DAO would be an interface and the intefaces would be implemented by whatever objects are injected. This allows you to unit test your service using dummy DAOs that implement the interfaces and then later implementing the DI with the actual DAO objects and doing your integration testings.

And that is why I love Spring!


Ran
Sai Hegde
security forum advocate
Ranch Hand

Joined: Oct 26, 2010
Posts: 200
    
    1

Coupling refers to the degree of direct knowledge that one class has of another. The less your components are tied to each other, the better. Using a DI framework has many benefits -
Dependencies are handled through the provider framework making your application code more flexible, reusable and scalable.
Using a DI Framework to manage the singletons in your applications. Singletons without a DI framework may hamper your testing.
Encourages the Program to interfaces approach.
Makes for an excellent architectural glue providing consistencies in how you would configure JDBC, JMS, Javamail, JNDI and any other API's.

Jayr Motta
Ranch Hand

Joined: Jul 30, 2010
Posts: 110

Dependency Injection is an awesome concept and we could talk about lots of benefits that it brings! This improves the cohesion once the code instead worry getting/instantiating objects they just do what they need to do ..

Ran said:
... The field type for each DAO would be an interface and the intefaces would be implemented by whatever objects are injected. ...


This will improve either on modularity of your software, i could say that your software is healthier ..


Feel free to ask me anything!
www.BlackBeltFactory.com/ui#!/ref=jmotta, SCJP 6, OCWCD JEE5, OCE EJB JEE6
 
GeeCON Prague 2014
 
subject: Have I achieved dependency Injection?