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
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:
Thanks and Regards,