Saturday, February 26, 2011

What are the benefits of IOC (Dependency Injection)

What are the benefits of IOC (Dependency Injection)
Benefits of IOC (Dependency Injection) are as follows:
·   Minimizes the amount of code in your application.
·   With IOC containers you do not create your objects but describe how they should be created. You can also easily add additional services by adding a new constructor or a setter method with little or no extra configuration.
·   Make your application more testable by not requiring any singletons(i.e a class with only one object) or JNDI(.i.e java naming directory interface) lookup mechanisms in your unit test cases. IOC containers make unit testing and switching implementations very easy by manually allowing you to inject your own objects into the object under test.
·   IOC supports Loose coupling
·   IOC containers support lazy loading of services



Lazy loading:
when a Java program is compiled it does not know that whether the pointer of a Parent class is either pointing to a parent object or child object, hence it will not bind the methods with pointers at the time of compiling it will wait for runtime to attach methods with pointers this is also called dynamic method dispatch this 'attaching' happens on every method call during runtime
 2nd type of lazy loading is:  remote method invocation
  3rd type:  Anonymous inner class
  4th type: use of 'Class' class

What are the different types of IOC (dependency injection)


What are the different types of IOC (dependency injection)

There are three types of dependency injection:

·   Constructor Injection (e.g. Pico container, Spring etc): This will injects the dependency via a constructor.
In the class OutputHelper               IOutputGenerator outputGenerator; 
                        OutputHelper(IOutputGenerator outputGenerator){
                                     this.outputGenerator = outputGenerator;       }
In the xml file <bean id="OutputHelper" class="com.mkyong.output.OutputHelper">
                                     <constructor-arg>
                                                 <bean class="com.mkyong.output.impl.CsvOutputGenerator" />
                                     </constructor-arg>
                        </bean>
·   Setter Injection (e.g. Spring): This is the most popular it will injects the dependency via a setter method.
In the class OutputHelper               IOutputGenerator outputGenerator; 
                        public void setOutputGenerator(IOutputGenerator outputGenerator){
                                    this.outputGenerator = outputGenerator;       }
In the xml file  <bean id="OutputHelper" class="com.mkyong.output.OutputHelper">
                                    <property name="outputGenerator" ref="CsvOutputGenerator" />
                                    </property>
                        </bean>
·   Interface Injection (e.g. Avalon): Injection is done through an interface.
Note: Spring supports only Constructor and Setter Injection