Thursday, November 1, 2012

Inversion of Control




IoC is one of the techniques used to wire services or components to an application program.

By definition, IoC is “A software design pattern and set of associated programming techniques in which the flow of control of a system is inverted in comparison to the traditional interaction mode.” 


In Java there are several basic techniques to implement inversion of control. These are:
1. using a factory pattern
2. using a service locator pattern
3. using a contextualized lookup
4. using a dependency injection,
 for example:
·         a constructor injection
·         a setter injection
·         an interface injection


Simply stated, in IoC, instead of an application calling the framework, it is the framework that calls the components specified by the application.This approach is similar to the one that Hollywood agents adopt with their clients. It is sometimes known as “Don’t call me, I will call you.” This is why IoC is also known as the Hollywood approach.

IoC is a broad and generic term

There are three forms or styles of Dependency Injection. 
The aspect of IoC that the Spring Framework uses is "Injection of required resources or dependency at Run-time into the dependent resource," which is also known as Dependency Injection. 

Hence, the service provided by the IoC container of Spring is Dependency Injection. 


They are:
1.  Constructor Injection [Spring Framework directly supports]
2.  Setter Injection [Spring Framework directly supports]
3.  Interface Injection [Spring Framework indirectly supports]


1.  Constructor Injection: In Constructor Injection, an IoC container uses the constructor to inject the dependency. All the dependencies, whether they are simple values or references to other objects, are declared in the constructor. One of the advantages of Constructor Injection is that all the dependencies are declared in one go. This also helps in understanding whether the class depends on too many services.

2.  Setter Injection: This form of Dependency Injection uses Setters, also known as mutators (because they change the value of the corresponding instance variables), to inject the required resources or dependencies. In other words, each of the objects that the class depends upon will have a setter and the IoC container will use the setters to provide the resource at run-time.

The main difference between Constructor Injection and Setter Injection 
Constructor Injection, the handing over of the dependencies takes place during instantiation of the dependent object
 Setter Injection, the handing over of the dependencies takes place after instantiation of the dependent object

 The Spring Framework favors Setter Injection over Constructor Injection.

3.  Interface Injection: Interface Injection provides the concrete implementations of an interface to the dependent object according to the configuration. The main difference between Interface Injection and the previous two is that in Interface Injection, any of the implementations of the interface can be injected, whereas with the other two, the object of the class specified is injected. Spring does not provide direct support for Interface Injection.


When to use Dependency Injections?
There are different scenarios where you Dependency Injections are useful.
  • You need to inject configuration data into one or more component.
  • You need to inject the same dependency into multiple components.
  • You need to inject different implementation of the same dependency.
  • You need to inject the same implementation in different configuration.
  • You need some of the services provided by container.
When you should not use Dependency Injection?
There were scenarios where you don’t need dependency injections e.g.
·         You will never need a different implementation.
·         You will never need different configurations.



No comments:

Post a Comment