Inversion of Control and Dependency Injection

Is IOC and DI same?
This is one of the question that pops up for many people. The Spring framework and EJB3.0 have really made it complex to actually understand the difference between the two.

IOC
Inversion of Control is a general term first used to explain how OO frameworks work. Inversion of Control is based on the Hollywood principle "Don't call us, We will call you". This can be seen evidently in JUnit test cases, EJB call back methods and even seen explicitly in the template design pattern. In JUnit test cases IOC can be seen in the execution of setup() and destroy(). EJB call back methods are good example where the control is given to the callback method.

DI
The basic principle behind Dependency Injection (DI) is that objects define their dependencies (that is to say the other objects they work with) only through constructor arguments, arguments to a factory method, or properties which are set on the object instance after it has been constructed or returned from a factory method.
DI is one form of IOC

Comments

Popular Posts