Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between "Inversion of Control", "Dependency inversion" and "Decoupling"

I'm reading theory about dependency inversion and decoupling and I can't see the difference between the two.

Dependency inversion talks about decoupling functional components so that higher level components don't depend on lower level components.

Decoupling talks about the same thing and how to achieve it. But then we have IoC Containers that mess things up even further. Why aren't they rather called Dependency Inversion Containers or even better Dependency Injection Containers, because they serve runtime coupling of independent components?

Then we have Inversion of Control. It's basically the same thing as Dependency Inversion isn't it? Why are there three terms that describe the same thing? Or am I blind?

  1. What is the difference between the three?
  2. What does IoC have to do in IoC Containers?
like image 568
Robert Koritnik Avatar asked Oct 12 '10 07:10

Robert Koritnik


People also ask

What is the difference between dependency inversion and Inversion of Control?

The Inversion of Control is a fundamental principle used by frameworks to invert the responsibilities of flow control in an application, while Dependency Injection is the pattern used to provide dependencies to an application's class.

What is difference between IoC and DI?

Inversion of Control (IoC) refers to a programming style where a framework or runtime, controls the program flow. Inversion of control means we are changing the control from normal way. It works on Dependency Inversion Principle. DI is a software design pattern that allow us to develop loosely coupled code.

What is Inversion of Control?

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.

What is the relationship between Inversion of Control and dependency injection?

Inversion of Control is a design concept that enables the creation of dependent objects to be inverted. On the flipside, Dependency Injection, a software architectural pattern, is an implementation of the Inversion of control principle.


3 Answers

Decoupling is a very general principle applicable in many fields. Dependency inversion is a specific form of decoupling where you decouple the higher levels of your system from the lower levels by separating them into libraries and using interfaces. This allows you to replace lower level parts of your system without major rework.

For example, instead of the higher level parts of the system creating concrete instances of the lower level classes, an IoC container can be used to decouple how objects are created.

Inversion of control is a design principle used by framework libraries that allow the framework to regain some control from the application. I.e., a windowing framework may call back into application code when certain user interface events occur. Martin Fowler uses the term Hollywood Principle as in Don't call us, we'll call you. Decoupling is an important part of inversion of control.

But what has an IoC container to do with inversion of control? To quote Martin Fowler:

Inversion of Control is too generic a term, and thus people find it confusing. As a result with a lot of discussion with various IoC advocates we settled on the name Dependency Injection.

(Note that Martin Fowler talks about dependency injection, not dependency inversion.)

An IoC container helps to implement dependency injection and perhaps a better term would be dependency injection container. However, the IoC container name seems to stick. Dependency injection is an important component in dependency inversion, but the use of IoC containers for dependency injection can be confusing as inversion of control is a broader and more generic principle.

You point out that the naming isn't very consistent but that shouldn't be a big surprise as these terms have been independently invented and used even though they overlap.

like image 110
Martin Liversage Avatar answered Oct 05 '22 20:10

Martin Liversage


Dependency Injection achieves Decoupling using Inversion of Control.

like image 21
Boris Pavlović Avatar answered Oct 05 '22 22:10

Boris Pavlović


I find the following explanation from DIP in the Wild article on martinfowler.com straightforward to understand (here DI = Dependency Injection, DIP = Dependency Inversion Principle, IoC = Inversion of Control):

DI is about how one object acquires a dependency. When a dependency is provided externally, then the system is using DI. IoC is about who initiates the call. If your code initiates a call, it is not IoC, if the container/system/library calls back into code that you provided it, is it IoC.

DIP, on the other hand, is about the level of the abstraction in the messages sent from your code to the thing it is calling. (...) DI is about wiring, IoC is about direction, and DIP is about shape [of the object upon which the code depends].

like image 26
hidro Avatar answered Oct 05 '22 21:10

hidro