Why use IoC

Lets see one example where code (caller has complete control over callee) has complete control against the philosophy of  IoC –

public class AgainstIoc {

    public List<String> getAllNonIoCProgrammers() {

    ProgrammingDaoImpl programmingDaoImpl = new ProgrammingDaoImpl();


public interface ProgrammingDao {

    public List<String> getAllNonIoCProgrammers();


public class ProgrammingDaoImpl implements ProgrammingDao {

   public List<String> getAllNonIoCProgrammers(){

   //gets a list from of NoNIoCProgrammers fromDb



Here above code has complete control on the flow of code.It knows it is calling getAllNonIoCProgrammers() method from Concrete class ProgrammingDaoImp .Later if I want to add a new Implementation of ProgrammingDao ,every  where I have to go and change for instantiation of new class .This would be too much in a big Project where I have to go change at many lines.

Lets now use IoC -using construction Injection  —

 public class IocWay {

   private ProgrammingDao programmingDao;
       //Inversion of control by constructor injection
       public IocWay(ProgrammingDao programmingDao){

                            this.programmingDao = programmingDao;


   public List<String>  getAllNonIoCProgrammers() {
      //here caller does know about callee,it has no control over callee

In this way code does not  have the control to whom it is calling .Caller does not have  a control over calllee.Caller just know thats eventually it would get a response  from callee.So now we can have different implementation of  ProgrammingDao ,one for development ,another for testing or different one for production ,caller does not have to worry about it because it does not know anything about callee, it does not have a control over callee.

This entry was posted in Design and tagged . Bookmark the permalink.

4 Responses to Why use IoC

  1. Christian says:

    Dependency Injection is only one (important) special case of Inversion of Control, but it sounds like IoC == DI.

  2. I think, this is just the use of polymorphism. And as far as I know, Dependency Injection (DI) is another name for Inversion of Control (IoC). Please have an opportunity if I am wrong.

    • ratn says:

      DI is not another name of IoC.This example tries to demonstrate how would you create an Instance of an Interface(High level component ) in runtime using IoC principle that is DI in that case .Also you can read about how Dependency Injection and Dependency Inversion are related to IoC.In this example Dependency Injection and Inversion both are used .We can discuss it further ….

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s