Introduction to Inversion of Control

Inversion of Control is a principle in software engineering by which the control of objects or portions of a program is transferred to a container or framework. It’s most often used in the context of object-oriented programming.

In traditional programming, our custom code makes calls to a library of some company like spring or apache or any third party, IOC enables a framework to take control of the flow of a program and make calls to our custom code. To enable this, frameworks use abstractions with additional behavior built in.

If we want to add our own behavior, we need to extend the classes of the framework with our own classes.

  • Decoupling the execution of a task from its implementation.

  • Making it easier to switch between different implementations.

  • Greater modularity and readability of a program.

  • Greater ease in testing a program by isolating a component or mocking its dependencies. We can use junit in this case for unit testing.

Inversion of Control can be achieved through various mechanisms such as: Strategy design pattern, Service Locator pattern, Factory pattern, and Dependency Injection (DI).