设计原理

发布于 2024-10-10 11:39:31 字数 117 浏览 2 评论 0原文

下面两种设计原则有什么区别?

  1. “针对接口编程,而不是实现”和
  2. “依赖于抽象,不依赖于具体类”。

这两个原则说的是同一件事,但以两种不同的方式。

what is the difference between below two design principles?

  1. " program to an interface, not an implementation" and
  2. " depend upon abstraction, do not depend upon concrete class".

these two principle saying the same thing but in two different ways.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

朱染 2024-10-17 11:39:31

接口是具体类的抽象,因此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).

ぽ尐不点ル 2024-10-17 11:39:31

他们本质上是用不同的词说同样的事情。

您应该编写您的类,使其依赖于抽象想法(如接口),而不是想法的具体实现。这允许您分段更改行为,而不必重新编写整个代码块。

请参阅依赖注入。

示例:

public class Chef : IResturauntWorker 
{
    // This is an example of writing to an interface instead of an
    // an implementation.  Because the Chef class implements the 
    // IResturauntWorker interface, the chef can be swapped in with other
    // resturaunt workers like bussers or waiters.

    public void Chop(Carrot c)
    {
        // code to chop a carrot

        // this is an example of depending on an implementation
        // because Carrot is a concrete class, this method can 
        // only be used with a Carrot
    }

    public void Chop(IVegetable c)
    {
        // code to chop a Vegetable

        // this is an example of depending on an abstraction
        // because IVegetable is a abstraction of an idea 
        // (a vegetable, in this case), this method can be
        // used with any class that implements IVegetable,
        // including carrots, celery, onions, etc.
    }
}

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:

public class Chef : IResturauntWorker 
{
    // This is an example of writing to an interface instead of an
    // an implementation.  Because the Chef class implements the 
    // IResturauntWorker interface, the chef can be swapped in with other
    // resturaunt workers like bussers or waiters.

    public void Chop(Carrot c)
    {
        // code to chop a carrot

        // this is an example of depending on an implementation
        // because Carrot is a concrete class, this method can 
        // only be used with a Carrot
    }

    public void Chop(IVegetable c)
    {
        // code to chop a Vegetable

        // this is an example of depending on an abstraction
        // because IVegetable is a abstraction of an idea 
        // (a vegetable, in this case), this method can be
        // used with any class that implements IVegetable,
        // including carrots, celery, onions, etc.
    }
}
祁梦 2024-10-17 11:39:31

接口只不过是提供通信不同类型的实现的方法
抽象不过是用一些抽象方法创建泛型类。

Interface is nothing but providing mean to communicate diffrent types of implementation
Abstraction nothing but creating generic class with some abstract method.

铃予 2024-10-17 11:39:31

在面向对象的世界中,这些术语经常互换使用。
接口只是抽象,而实现只是具体。

In the object-oriented world, these terms used frequently and interchangeably.
Interface is nothing but abstraction and implementation is nothing but concrete.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文