Sunday, 21 February 2010

Dependency Injection


If the underlying server or container or framework or Runtime environment dynamically assigns values to the resource (Example: classes or objects) then it is called dependency Injection.
In this underlying server or container pushes the values to resource (class).

Ex:
The way JVM calls constructor to initialize object when object is created is called as dependency Injection.
Ex2:
If course material assigned to student the moment he registers for course is called dependency injection

There are two modes dependency injections
(a)    Setter injection

(b)   Constructor injection.

In details:


Dependency Injection (DI):
Problems if we dont use dependency injection
 leads to tightly coupled.
1. Replacing Tea class with another class say ExpressTea class.
2. Modifying the Tea class say parameterising its constructor.
3. Testing becomes difficult . you cant test your project without including dependencies class
You can not test Restaurent class without including Tea class bcoz Restaurent class is a direct dependency on Tea class.

Class Reastaurent
{

 Tea tea=new Tea();//u are creating an objct in another object
  public void prepareHotDrink()
{
 tea.prepareTea();
 }            
}
class Tea
{
 p v prepareTea()
 {
  //code here to prepare tea
 }
}

Solutions is Dependency Injection will solve the problem:
1. you will not mention Tea object inside the Restaurent class and still the restaurent class will use Tea object

Dependency Injection says never create an object inside the java class using a new operator.

The Restaurent class will use the Tea class without creating Tea object inside the Restaurent class using this principle enchances Code readability,
 code becomes more managable.

The Spring framework is more popular of Depencency Injection

Definition:

To inject dependency of an object from outside.

Real Time example\;

If you take Tea cup:

it depends on
1. tea powder
2. milk
3. sugar
4. cup
5. kettle
Here if u want to prepare the tea object
1. you prepare tea object yourself and then drink it
2. you ask an expert to prepare tea and you just enjoy it.

Here the second option is best: because that expert prepares all the dependencies, and serves you 

here the same concept applies in coding also. Here the tea expert means out sourcing (3rd party) program.

So when the Restaurent object is created, all the dependencies would be created and provided to it from the outside by such a third party program.
Here you no need to use such a third party program ,
In order to use such a third party program you need to change the class is designed also,
just introducing interface in your project. You will use an interface which will bind the Restaurent and Tea class .

Now let's write an interface Hotdrink
Example:

interface IHotDrink
{
 public void prepareTea();

}


class Restaurent
{
 IHotDrink hotdrink;
 public void prepareHotDrink()
{
 hotdrink.prepareTea();
 }                            
}

class Tea implements IHotDrink
{
public void prepareTea(){
//code here to prepare tea
}
}

Now the Restaurent class will use this interface instead of Tea class object .
And the Tea class will implement this interface IHotDrink
In the Reastaurent class you will write a constructor or a setterMethod()

interface IHotDrink
{
 public void prepareTea();

}


class Restaurent
{
 IHotDrink hotdrink;
 //Initialise the IHotDrink interface with the tea object
 Restaurent(IHotDrink hotDrink)
 {
  hotDrink=this.hotDrink;
 }
 public void prepareHotDrink()
{
 hotdrink.prepareTea();
 }                            
}

class Tea implements IHotDrink
{
public void prepareTea(){
//code here to prepare tea
}
}


/*
//using setters method instead of constructor


class Restaurent
{
 IHotDrink hotdrink;
 //Initialise the IHotDrink interface with the tea object
 public void setIHotDrink(IHotDrink hotDrink)
 {
  hotDrink=this.hotDrink;
 }

 public void prepareHotDrink()
 {
  hotdrink.prepareTea();
 }                            
}

*/
Now we had made all the design changes
Now you simply instruct this 3rd party program which supports DI that at run time when the Restaurent class object is created,
it should create and inject the Tea class object into the Restaurent class through the "Constructor" argument or setterMethod().

If you using spring framework this instruction you provide in a configuration file provided by spring and if you are using some other
3rd party program you provide the instruction the way that program accepts it thats all about dependency injection (DI).


How SpringFramework provides DI

Ans: Spring Framework provides two ways of DI:

1. Using the constructor way.
2. using the setter method way. 



No comments:

Post a Comment