0 votes
in Full Stack Developer by
What is inversion of control?

2 Answers

0 votes
by

In contrast with conventional control flow, IoC (Inversion of Control) inverts the flow of control. In IoC, a generic control flux is given to custom-written portions of a computer program. In conventional programming, the custom coding that communicates the program’s intent, calls on reusable libraries to carry out a generic function. Still, in reverse control, it is the frame to call in the custom or task-specific code. The software architecture has the same design and reverses the traditional procedural 

programming method.

0 votes
by

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.

...