Dependency Injection

In computer science dependency injection is a process which is dynamically load class needed objects references in run time. This scenario achieves loosely coupling and dynamic reference change when you do programming. Today I am going to explain what is dependency injection (DI) and what are the pros and cons of DI. Before you start learning on DI you have to learn the theory of Inversion of control. 

First of all please subscribe our YouTube channel for more tutorial with programming examples.
https://www.youtube.com/channel/UCiP3dlCCZ2Sx6CUyQ7W_vaw?view_as=subscriber

Inversion of Control

Inversion of control (IoC) is a design principle which support to invert dependancies of class from external world without tightly coupling. Program no need to wait to load all of your class dependancies and there are few design patterns we can achive IoC. DI is a famous design pattern which I am going to explain in this article. 

Below I am going to show a traditional code where coupling is pretty high.

public class Currency {
public String getCurrencySign(String country) {
if(country.equalsIgnoreCase("USA")){
return "$";
}
return "NA";
}
}
public class NewYorkIndex {
Currency currency;
NewYorkIndex(){
currency = new Currency();
}
private void displayIndexAtDayStart(){
System.out.printf(currency.getCurrencySign("$"));
}
}

 

This example shows that we have to create new object from Currency class and NewYorkIndex class is tightly couple with Currency  class object. What Inversion of Control does is make this scenario loosely coupling enable and reducing static dependencies in NewYorkIndex. 

Now I am going to show how we use loosely coupling and achieve DI from below code same scenario. Basically high level and low level class should not have tightly coupling and high level and low level classes and data should be coupled with abstraction only.  Now I am going to add code snippet for DI. 


public interface ICurrency { public String getCurrencySign(); } public class CurrencyUSD implements ICurrency { public String getCurrencySign() { return "$"; } }

public class CurrencyEuro implements ICurrency {
public String getCurrencySign() {
return "€";
}
}
public class CurrencyFactory {
    public static ICurrency getCurrency(String country){
        if (country.equals("USA")) {
            return new CurrencyUSD();
        } else if (country.equals("UK")) { 
return new CurrencyEuro();
} else { return new CurrencyUSD(); } } } public class NewYorkIndex { ICurrency currency; NewYorkIndex(){} private void displayIndexAtDayStart(){ System.out.printf(CurrencyFactory.getCurrency("USA").getCurrencySign());
System.out.printf(CurrencyFactory.getCurrency("UK").getCurrencySign()); } }

 

This code you can see CurrencyFactory class create objects of different currency classes and NewYorkIndex class dynamically injecting currency object as this class need. Like wise now any low level class will not need to create new instances of any currency classes but they will be injected via CurrencyFactory when low level class need. 

Please comment your ideas and question below.

Thank you.

 

 

Leave a Reply