是“依赖倒置"吗?和“设计到接口"相同的原则? [英] Are "Dependency Inversion" and "Design to Interfaces" the same principles?
问题描述
依赖倒置原则"(DIP) 和接口设计原则"是否表达了相同的原则?如果不是,那会有什么区别?
Do the "Dependency Inversion Principle" (DIP) and "Design to Interfaces Principle" express the same principle? If not, what would be the difference?
编辑
为了澄清和缩小上下文范围:我所说的接口是指一个编程接口,就像 Java interface
或 C++ 中的纯抽象基类.不涉及其他合同".
To clarify and narrow down the context a bit: by interface I mean a programmatic interface, like a Java interface
or a pure abstract base class in C++. No other 'contracts' are involved.
推荐答案
我只是想在 另一个与这个问题非常相似的问题,因为在我看来它确实很好地回答了这个问题.
I just wanted to pitch in and quote Derek Greer on another question very similar to this one, since it does answer this question nicely, in my opinion.
依赖倒置原则没有指的是通过使用接口(例如MyService → [ILogger ⇐ Logger]
)抽象依赖的简单做法."em>
"What the Dependency Inversion Principle does not refer to is the simple practice of abstracting dependencies through the use of interfaces (e.g.
MyService → [ILogger ⇐ Logger]
)."
虽然这将组件与依赖项的具体实现细节解耦,但它不会反转使用者和依赖项之间的关系(例如 [MyService → IMyServiceLogger] ⇐ Logger
)."
While this decouples a component from the specific implementation detail of the dependency, it does not invert the relationship between the consumer and dependency (e.g. [MyService → IMyServiceLogger] ⇐ Logger
)."
这篇关于是“依赖倒置"吗?和“设计到接口"相同的原则?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!