Dependency Injection design pattern

Posted on by By Nikhilesh, in Miscellaneous | 0

This blog is about one of the hot topics in programmers community. It is Dependency Injection or Inversion of Control.

What is Dependency Injection?

Here is a one liner from James Shore:

“Dependency injection means giving an object its instance variables.”

Here is the source for the above definition: Dependency Injection Demystified 

Formal definition:

Dependency injection is basically providing the objects that an object needs (its dependencies) instead of having it construct them itself in order to achieve loose coupling. An injection is the passing of a dependency (a service) to a dependent object (a client). This process is fundamentally the inverse, hence the name Inversion of Control (IoC), of the object itself controlling the instantiation or location of its dependencies on its own by using direct construction of classes. It's a very useful technique for testing, since it allows dependencies to be mocked or stubbed out.

What is wrong about using ‘new’ keyword in your java code?

Using the ‘new’ keyword in your code couples the objects tightly and makes them ‘aware’ of the other objects i.e. their dependencies.

Why all this buzz?

It’s all about loose coupling of the code. When the application objects are loosely coupled or in other words when the application objects do not know much about the objects that they are dependent up on, the application will be very modular. The code will be cleaner. We can change any object that is malfunctioning or having inadequate functionality or buggy at any point in time. In production environment this is very important. Each time when we change the code, it involves a lot of testing. If we are changing a lot of code then it may be a necessary evil. But, if we want to change only a smaller portion of code or component then it will be great to change only that particular code, unit test it and integrate it. This is possible only when we have loosely coupled objects in our application.

In software industry the only constant thing is change. The clients always have changes in requiremets. Hence using DI will be a good design choice.

DI exists in two major variants, Constructor-based dependency injection and Setter-based dependency injection. In constructor based injection the dependencies supplied to the constructor as arguments so that the instance variables are set with their dependencies. In setter based injection the dependency is passed as the setter method argument so that the instance variable can be set with its collaborating object. Using constructor based injection is preferable to the later one because the instance variables are ensured to be not ‘null’ to avoid the infamous ‘NullPointerException’. Setter based injection is preferred when we have to wire the optional variables.

Huh!, it’s a lots of theory right?. So, it is better to jump into some coding to understand the concept properly. Here are a few good sources (some of the content in this blog is also collected from the same sources) to learn about DI.

For further reading and understanding:

  • http://stackoverflow.com/questions/130794/what-is-dependency-injection
  • http://stackoverflow.com/questions/2407540/what-are-the-downsides-to-using-dependency-injection
  • http://docs.spring.io/spring/docs/4.1.1.RELEASE/spring-framework-reference/htmlsingle/#beans-factory-collaborators

Thanks and Regards,

Rajasekhar

Helical IT Solutions

logo

Best Open Source Business Intelligence Software Helical Insight is Here

logo

A Business Intelligence Framework

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments