“依赖倒置”中的“倒置”是什么意思 [英] What is meant by 'Inversion' in Dependency inversion

查看:273
本文介绍了“依赖倒置”中的“倒置”是什么意思的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习春天。我理解依赖注入。在某些地方我也看到它称为依赖倒置。我知道为什么它被称为注射,但是反转是什么意思?它实际上反转了哪种依赖?

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?

推荐答案

好问题 - 单词反转有点令人惊讶(因为,在应用 DIP 之后,较低级别的依赖模块显然没有'现在依赖在更高级别的调用者模块上 - 调用者和依赖者现在只是通过额外的抽象更松散地耦合了。)

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


有人可能会质疑为什么我使用inversion这个词。坦率地说,这是因为更多传统的软件开发方法,例如结构化分析和设计,倾向于创建软件结构,其中高级模块依赖于低级模块,其中抽象依赖于细节。实际上,这些方法的目标之一是定义子程序层次结构,该层次结构描述了高级模块如何调用低级模块。 ...因此,一个精心设计的面向对象程序的依赖结构相对于通常由传统程序方法产生的依赖结构是反转的。

One might question why I use the word "inversion". Frankly, it is because more traditional software development methods, such as Structured Analysis and Design, tend to create software structures in which high level modules depend upon low level modules, and in which abstractions depend upon details. Indeed one of the goals of these methods is to define the subprogram hierarchy that describes how the high level modules make calls to the low level modules. ... Thus, the dependency structure of a well designed object oriented program is "inverted" with respect to the dependency structure that normally results from traditional procedural methods.

在阅读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.

编辑
只是为了澄清:

Edit Just to clarify:


在某些地方我也看到它称为依赖倒置

请注意依赖注入(DI)是实现依赖性倒置原则(DIP)的可能实现之一 - SOLID设计原则中的D,所以 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.

这篇关于“依赖倒置”中的“倒置”是什么意思的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆