What is Inversion of Control?
One of the major principles of Software Engineering is that classes should have minimum interdependence, i.e., low coupling. Inversion of Control (IoC) is a design principle that allows classes to be loosely coupled and, therefore, easier to test and maintain. IoC refers to transferring the control of objects and their dependencies from the main program to a container or framework. IoC is a principle, not a design pattern – the implementation details depend on the developer. All IoC does is provide high-level guidelines.
Inversion of Control and Dependency Injection are often used interchangeably. However, Dependency Injection is only one implementation of IoC. Other popular implementations of IoC are:
What is dependency injection?
Dependency injection is a technique that allows objects to be separated from the objects they depend upon. For example, suppose object A requires a method of object B to complete its functionality. Well, dependency injection suggests that instead of creating an instance of class B in class A using the new operator, the object of class B should be injected in class A using one of the following methods:
Recommended by LinkedIn
What is the service locator pattern?
The service locator pattern includes service locator object that contains information about all the services that an application provides. Upon instantiation, the services register themselves with the service locator. Once it receives a request from a client, the service locator performs the required task and returns the needed values. This, also, reduces coupling between the dependent classes.
What is Spring Framework?
A great example of an implementation of IoC is Spring Framework. The Spring container instantiates and manages the lifecycle of the objects that are a part of the program. The user in the configuration file provides the information related to what objects and dependencies are required by the application. This file may be one of the following: