【问题标题】:What is the difference between DI principle and "Program to interface, not to implementation"?DI原则和“程序到接口,而不是实现”有什么区别?
【发布时间】:2014-04-24 22:15:22
【问题描述】:

我不明白依赖倒置与 Gof 书中著名的短语“程序到接口,而不是实现”之间的区别。 DIP 的定义阐明了以下原则:

  1. 高级模块不应依赖于低级模块。两者都应该依赖于抽象。
  2. 抽象不应依赖于细节。细节应该取决于抽象。

似乎这两个原则都在做同样的事情:将接口与实现分离。

【问题讨论】:

    标签: oop inversion-of-control


    【解决方案1】:

    “程序到接口,而不是实现”是 OOP 中一般意义上的一个很好的建议(即使您的语言不支持接口的概念)。这个想法是,发送消息的对象不应该关心接收者的细节(例如,哪个类是实例或者它是否属于给定的层次结构),只要它可以回答一组消息(并因此执行一组行为)。如果您查看 GoF 中的模式,一个主要的底线是,只要您针对一个接口进行编程,您就可以用另一个目标对象替换目标对象,而无需在客户端中进行任何更改。

    关于Dependency Inversion Principle,我认为它只是对前一个想法的具体应用。在分层架构的上下文中,您正在将编程的想法应用于接口而不是具体类,目的是将较低层与较高层解耦,以获得灵活性和可重用性。

    HTH

    【讨论】:

      【解决方案2】:

      假设您有一个Computer class 定义如下:

      public class Computer
      {
          public string OwnerName{get; set;}
      
          public int RAMCapacity{get; set;} //Hardware based property
      
          public string OperatingSystem{get; set;} //Software based property
      }
      

      现在,对Interface 的编程表明,按照上面的代码 cmets,您应该创建一个 ISoftwareComponentsIHardwareComponents 接口并将这些属性移动到各自的接口并在 Computer 类中实现这两个接口,如下所示:

      public interface ISoftwareComponents
      {
          string OperatingSystem{get; set;}
      }
      
      public interface IHardwareComponents
      {
          int RAMCapacity{get; set;}
      }
      
      
      public class Computer : ISoftwareComponent, IHardwareComponents
      {
          public string OwnerName{get; set;}
      
          public int RAMCapacity{get; set;} //IHardwareComponents property
      
          public string OperatingSystem{get; set;} //ISoftwareComponents property
      }
      

      现在Computer 类的客户端代码可以使用如下代码:

      Computer comp = new Computer();
      
      //software requirements can use the below code:
      string os = ((ISoftwareComponents)comp).OperatingSystem; // and, other variations by method calls
      
      //hardware requirements can use the below code
      int memory = ((IHardwareComponents)comp).RAMCapacity; //and, other variations
      

      您也可以只将计算机的软件和硬件接口部分传递给其他类和方法,如下所示:

      public void SetHardware(IHardwareComponents comp)
      {
          comp.RAMCapacity = 512;
      }
      

      进一步了解上述示例,您会了解更多。

      【讨论】:

        猜你喜欢
        • 2012-03-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-02-07
        • 1970-01-01
        • 2021-07-10
        • 2018-08-16
        • 2011-01-25
        相关资源
        最近更新 更多