Anand's programming Blog

Thoughts and comments on Java software development.

Thursday, February 02, 2006

IOC and Dependency Injection - Simplified

Subject pattern is being thrown out in every Party and Kitchen/Water-cooler discussions now a days. Its not cool, if you have not heard about one of these buzz word. I am trying an attempt to simplify what this design pattern means and why IOC vs DI. For more in-depth information you should refer to Martin Flower's website.

I will use terms (A) and (B)below, it represents two decoupled applications/component.

Inversion of Control ( IOC): This should be interpreted as an application( A ) handing off control of logic/action to other component/application ( B). Martin Flower explains this in a very simplistic Hollywood slang " Don't call me, I will call you". Here B is saying to A that I have control over this part you worry about other things. This pattern is no new invention, the concept of IOC is very generic and used in day today software development even if you never heard the 3 letter acronym.. Good example is Java EventHandlers. Now where does DI - dependency injection comes into picture....

Dependency Injection ( DI): In simple terms, DI is specialized IOC, where in inversion of control deals with "plugin", thus the name injection-injecting the plugin. (A) application inverts/handsoff the responsibility of determining the plugin to B thus making itself decoupled and generic enough to be used by any client.

Types of DI: Client will still need to tell (B) what to plugin should be like. This is where different types of DI like setter based DI, constructor based DI etc. For example Spring framework use the setter based DI. A framework provides client an assembler mechanism to define and associate plugins. Like Spring provides XML configuration which is used to set appropriate methods of (B) with plugin.


That is all you need to know,if my explanation is still not simplistic enough. Try to remember these two

IOC: "Don't call me , I will call you"
DI: "Don't plugin yourself, I will do that"

0 Comments:

Post a Comment

<< Home