Inversion of control is a pattern used to decouple the dependencies between layers and components in the system. The Dependency-Injection (DI) pattern is an example of an IoC pattern that helps in removing dependencies in the code.
Let us understand this with the help of an example. Consider we have a class A that makes use of class B as shown below:
public class A{
private B b;
public A(){
this.b = new B();
}
}
Here, we have a dependency between classes A and B. If we had the IoC pattern implemented, we would not have used the new operator to assign value to the dependent variable. It would have been something as shown below:
public class A {
private IocB b;
public A(IocB b) {
this.b = b;
}
}
We have inverted the control of handing the dependency of instantiating the object of class B to the IoC class IocB.