问题描述
另一种提出这个问题的方法是:根据你的反转控制是什么?
Another way to ask this question is: what is Inversion of Control according to you?
我问这个问题是因为维基百科的文章被非OO解释劫持。来自,来自2007年: p>
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:
我没有看到Inversion of Control在OO语言之外有任何意义。已经有许多解释放弃过程语言的控制(事件编程是一个),纯功能语言不需要像反转控制一样的概念,因为它们具有更高级的功能。
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.
此外,在Martin Fowler阐述IoC的中,他专门处理OO示例。
Also, in the article where Martin Fowler elaborates on IoC he exclusively handles OO examples.
所以,IoC是一个OO概念,它是什么?
So, is IoC exclusively an OO concept, and what is it exactly?
对我来说,IoC尝试在大多数OO语言强加的限制内将函数转换为数据,并尝试将这些函数作为参数传递给其他函数。这不是IoC的唯一部分,但还有一些。
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.
对我来说,IoC是一个OO概念。
To me, IoC is exclusively an OO concept.
你的答案是什么?
推荐答案
从实现的角度看待理论问题。出现的第一个问题应该是究竟是什么控制你反转?
You look at the theoretical problem from a implementational viewpoint. The first question that arises should be exactly what Control do you Invert ?
然后你会意识到,它没有关系它的一个对象,方法,功能,或者传递的东西,但代码实际上是什么。
Then you will realize that it doesn't matter wether its an object, method, function or whatnot that is passed around, but what actually the code does.
简而言之,当你执行依赖注入时,你反过来控制创建和使用依赖关系(资源)。
In short, when you do Dependency Injection, you invert the control of the creation and usage of dependencies (of resources).
当给Windows API函数指向回调函数的指针时,您可以使用自己的参数来控制调用函数
When you give a Windows API function a pointer to a callback function, you give them the control of calling your function with their own parameters.
所以你看,IoC只是一个理论概念,而且,它可以有不同的实际实现。
So you see, IoC is just a theoretical concept, and ofcourse, there can be different practical implementations of it.
这篇关于对OO语言的反转控制是否具体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!