设计原理
下面两种设计原则有什么区别?
- “针对接口编程,而不是实现”和
- “依赖于抽象,不依赖于具体类”。
这两个原则说的是同一件事,但以两种不同的方式。
what is the difference between below two design principles?
- " program to an interface, not an implementation" and
- " depend upon abstraction, do not depend upon concrete class".
these two principle saying the same thing but in two different ways.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
接口是具体类的抽象,因此2.是1的子集。原则1.具有更广泛的适用性(您可以将其用于任何类型的接口,而不仅仅是面向对象编程中使用的接口)。
Interface is an abstraction of a concrete class, so 2. is a subset of 1. The principle 1. has wider applicability (you can use it for any kind of interface, not only those used in object-oriented programming).
他们本质上是用不同的词说同样的事情。
您应该编写您的类,使其依赖于抽象想法(如接口),而不是想法的具体实现。这允许您分段更改行为,而不必重新编写整个代码块。
请参阅依赖注入。
示例:
They are essentally saying the same thing in different words.
You should write your class so that it depends on an abstract idea (like an interface) instead of a concrete implementation of an idea. This allows you to change behavior in pieces instead of having to re-write whole chunks of code.
See Dependancy Injection.
Example:
接口只不过是提供通信不同类型的实现的方法
抽象不过是用一些抽象方法创建泛型类。
Interface is nothing but providing mean to communicate diffrent types of implementation
Abstraction nothing but creating generic class with some abstract method.
在面向对象的世界中,这些术语经常互换使用。
接口只是抽象,而实现只是具体。
In the object-oriented world, these terms used frequently and interchangeably.
Interface is nothing but abstraction and implementation is nothing but concrete.