Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Inversion of Control specific to OO languages?

Another way to ask this question is: what is Inversion of Control according to you?

I ask this question because the Wikipedia article on IoC has been hijacked by a non-OO explanation. This is taken from the discussion page and is from 2007:

I took the liberty to completely rewrite the page, as the previous content was completely taken over by meaningless "object oriented" babble ...

I don't see how Inversion of Control makes any sense outside of OO language. There are already many explanations for giving up control in procedural languages (event programming is one) and purely functional languages don't need a concept like Inversion of Control since they have higher-order functions.

Also, in the article where Martin Fowler elaborates on IoC he exclusively handles OO examples.

So, is IoC exclusively an OO concept, and what is it exactly?

To me, IoC tries to turn functions into data within the limitations that most OO languages impose, and tries to pass those functions-as-data as arguments to other functions. That's not the only part of IoC, but there's some of that.

There's also the factory design pattern, where trees of objects are being constructed and configured before being passed on.

To me, IoC is exclusively an OO concept.

What's your answer?

like image 527
Steven Devijver Avatar asked May 04 '09 15:05

Steven Devijver


People also ask

When should you not use inversion of control?

To summarize two of the points of this article: You should not use IoC/DI where encapsulation is important. You should not use IoC/DI where you cannot demonstrate concrete examples of how the complexity added through the use of IoC/DI is out weighed by the benefits of using IoC/DI.

What is the main principle of inversion of control?

Inversion of control is a software design principle that asserts a program can benefit in terms of pluggability, testability, usability and loose coupling if the management of an application's flow is transferred to a different part of the application.

What is the specific name of Inversion of Control pattern?

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: Service Locator Pattern.

What is the difference between Dependency Injection and inversion of control?

Dependency Injection is the method of providing the dependencies and Inversion of Control is the end result of Dependency Injection. IoC is a design principle where the control flow of the program is inverted. Dependency Injection is one of the subtypes of the IOC principle.


1 Answers

Inversion of control is most definitely not an OO concept.

IoC exists and is used quite frequently in non-OO languages. It is very common in C, for example. A prime example of this is the Windows API - any time you call any Windows API functions which work via callbacks, you're basically using IoC in it's most primitive form.

For example, take a look at the EnumWindows function. Using this, you pass a function pointer (EnumWindowsProc) to a library, and your code is run from within the library code.

Compare this to the definition of Inversion of Control from Wikipedia: "Inversion of control occurs when a library procedure calls user procedure(s)."

It's exactly the same.

However, IoC really becomes very powerful, flexible, and easy to use when you add a rich type system and many of the other tools that come with OOP. This makes it more common, since it's "nicer" to work with, but it did exist prior to OOP.

like image 183
Reed Copsey Avatar answered Nov 09 '22 16:11

Reed Copsey