7.12 其他面向对象设计原则3: 依赖倒置原则DIP

本文介绍了面向对象设计中的依赖倒置原则(DIP),包括其定义,即高层模块和低层模块都依赖抽象,抽象不依赖细节,细节依赖抽象。还阐述了使用该原则的原因,对比了过程化程序与面向对象架构,并给出依赖倒置原则的启发,如面向接口设计、避免传递性依赖等。

其他面向对象设计原则3: 依赖倒置原则DIP
   The Dependency Inversion Principle
7.1 依赖倒置原则DIP

  The Dependency Inversion Principle

    I. 高层模块不应当依赖低层模块 ,两者都依赖抽象

      High-level modules should not depend on low-level modules,Both should depend on abstractions

    II.抽象不能依赖细节,细节应当依赖抽象

      Abstractions should not depend on details, Details should depend on abstractions R. Martin, 1996

   引导

     继承层次关系中,基类不应当知道任何子类

      A base class in an inheritance hierarchy should not know any of its subclasses

     不能依赖一个有详细实现的模块,而这个模块本身也应当依赖抽象

      Modules with detailed implementations are not depended upon, but depend themselves upon abstractions

     OCP宣扬了目标,DIP宣扬了机制

      OCP states the goal; DIP states the mechanism
7.2 为什么依赖倒置原则DIP ?

   传统的面向过程的程序设计,以功能划分系统

     高层模块是业务/应用规则 High level modules: business/application rules

     低层模块是对这些规则的实现 Low level modules: implementation of the business rules

     高层模块完全依赖调用低层模块提供的功能来完成自己的功能

      High level modules complete their functionality by calling/invoking the low level implementation provided by the low level modules

   因此,高层依赖底层 High level depends on the lower level
7.2 为什么依赖倒置原则DIP ?
  比较:过程化程序与 面向对象架构
    过程化程序架构
    面向对象系统架构 Object-Oriented Architecture
7.3 依赖倒置原则的启发1
   使用继承,避免类之间的直接绑定 Use inheritance to avoid direct bindings to classes:
    面向接口设计,而不是面向实现设计 Design to an interface, not an implementation!
   为什么面向接口设计 Design to an Interface

     因为

       抽象类/接口修改的概率偏低 tend to change much less frequently

       抽象概念容纳的范围广,易于扩展/修改 abstractions are ‘hinge points’ where it is easier to extend/modify

       不应当修改代表抽象的类/接口,符合OCP原则 shouldn’t have to modify classes/interfaces that represent the abstraction (OCP)

       举例:中央的政策不能轻易修改,而乡镇的政策,错了马上改

     例外情况 Exceptions

       有些类非常成熟、稳定 Some classes are very unlikely to change

         插入抽象层,好处不多了,例如 String class,这里就可以直接使用具体类

         此时,不考虑依赖倒置的问题了
    避免传递性依赖 Avoid Transitive Dependencies
     使用继承机制,消除传递性依赖
     如果对自己设计的类找不到一个满意的解决方案,尝试把职责委 派其他一个或者多个类

      If you cannot find a satisfactory solution for the class you are designing, try delegating responsibility to one or more classes

    每当有疑虑时,增加一个间接层 When in doubt, add a level of indirection

转载于:https://www.cnblogs.com/mayZhou/p/10550064.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值