问题描述
我正在学习春天。我理解依赖注入。在某些地方我也看到它称为依赖倒置。我知道为什么它被称为注射,但是反转是什么意思?它实际上反转了哪种依赖?
I am learning spring. I understood dependency injection. In some place I also see it called dependency inversion. I got why it is termed as injection but what is meant by "inversion"? Which dependency is it actually inverting?
推荐答案
好问题 - 单词反转
有点令人惊讶(因为,在应用之后,较低级别的依赖模块显然没有'现在依赖
在更高级别的调用者模块上 - 调用者和依赖者现在只是通过额外的抽象更松散地耦合了。)
Good question - the word inversion
is somewhat surprising (since, after applying the DIP, the lower level dependency module obviously doesn't now depend
on the higher level caller module, either - both caller and dependency are now just more loosely coupled through an additional abstraction).
引用Robert C Martin的
Citing Robert C Martin's original source
在阅读Uncle Bob关于DIP的论文时要注意的一点是C ++没有(并且在编写本文时)具有接口,因此在C ++中实现这种抽象通常是通过抽象/纯虚拟基类实现的,而在Java或C#中,放松耦合的抽象通常是通过抽象来解耦来自依赖关系的接口,并将更高级别的模块耦合到接口。
One point to note when reading Uncle Bob's paper on the DIP is that C++ didn't (and at time of writing, still doesn't) have interfaces, so achieving this abstraction in C++ is typically implemented through an abstract / pure virtual base class, whereas in Java or C# the abstraction to loosen the coupling would usually be through decoupling by abstracting an interface from the dependency, and coupling the higher level module(s) to the interface.
编辑
只是为了澄清:
EditJust to clarify:
请注意是实现依赖性倒置原则(DIP)的可能实现之一 - ,所以 DI
和 DIP
不完全可以互换。
Note that Dependency Injection (DI) is ONE of the possible implementations to achieve the Dependency Inversion Principle (DIP) - the "D" in SOLID design principles, so DI
and DIP
are not entirely interchangeable.
其他DIP实现包括(现在是。
Other DIP implementations include the Service locator pattern (which is nowadays often regarded as an anti-pattern); and Plugin.
这篇关于“依赖倒置”中的“倒置”是什么意思的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!