Inversion of Control and Dependency Injection

MRINAL RANJAN
3 min readMar 14, 2021

--

Let me start by telling you that when you hear these two words you separate them but “Both are not different, Dependency Injection is a design pattern used to achieve Inversion of Control”.

Now let's understand what is Inversion of Control?

When you think about object-oriented programming the first thing which comes to your mind is objects and class. Now in traditional ways of programming to use other class properties, you create an object in your class.

But what if later you want to change it? You have to make changes in your class and that's where IOC comes into the picture you don't want to hard code your dependencies you want to be flexible, you don't want the control in your class hands best option will be Inversion of Control.

IoC is all about inverting the control. To explain this in layman’s terms, suppose you drive a car to your workplace. This means you control the car. The IoC principle suggests inverting the control, meaning that instead of driving the car yourself, you hire a cab, where another person will drive the car. Thus, this is called inversion of the control — from you to the cab driver. You don’t have to drive a car yourself and you can let the driver do the driving so that you can focus on your main work.

Dependency Injection

Dependency Injection (DI) is a design pattern used to implement IoC. It allows the creation of dependent objects outside of a class and provides those objects to a class in different ways. Using DI, we move the creation and binding of the dependent objects outside of the class that depends on them.

The Dependency Injection pattern involves 3 types of classes.

  1. Client Class: The client class(Consumer) is a class that depends on the service class
  2. Service Class: The service class(Provider) is a class that provides service to the client class.
  3. Injector Class: The injector class(IService) injects the service class object into the client class.

Types of Dependency Injection

As you have seen above, the injector class injects the service (dependency) to the client (dependent). The injector class injects dependencies broadly in two ways: through a constructor or through a property.

Constructor Injection: In the constructor injection, the injector supplies the service (dependency) through the client class constructor.

package com.coach;

public class BaseBall implements Coach {

private FortuneService fortuneService;

@Autowired

public BaseBall(FortuneService thefortuneService) {

fortuneService = thefortuneService;

}

}

Property Injection: In the property injection (aka the Setter Injection), the injector supplies the dependency through a public property of the client class.

package com.coach;

public class CricketCoach implements Coach {

private FortuneService fortuneService;

public CricketCoach{

}

@Autowired

public void setFortuneService(FortuneService fortuneService) {

this.fortuneService = fortuneService;

}

}

--

--