日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

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

發(fā)布時(shí)間:2023/11/27 生活经验 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 7.12 其他面向对象设计原则3: 依赖倒置原则DIP 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

其他面向?qū)ο笤O(shè)計(jì)原則3: 依賴倒置原則DIP
  ? The Dependency Inversion Principle
7.1 依賴倒置原則DIP

  The Dependency Inversion Principle

    I. 高層模塊不應(yīng)當(dāng)依賴低層模塊 ,兩者都依賴抽象

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

    II.抽象不能依賴細(xì)節(jié),細(xì)節(jié)應(yīng)當(dāng)依賴抽象

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

  ? 引導(dǎo)

    ? 繼承層次關(guān)系中,基類不應(yīng)當(dāng)知道任何子類

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

    ? 不能依賴一個(gè)有詳細(xì)實(shí)現(xiàn)的模塊,而這個(gè)模塊本身也應(yīng)當(dāng)依賴抽象

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

    ? OCP宣揚(yáng)了目標(biāo),DIP宣揚(yáng)了機(jī)制

      OCP states the goal; DIP states the mechanism
7.2 為什么依賴倒置原則DIP ?

  ? 傳統(tǒng)的面向過程的程序設(shè)計(jì),以功能劃分系統(tǒng)

    ? 高層模塊是業(yè)務(wù)/應(yīng)用規(guī)則 High level modules: business/application rules

    ? 低層模塊是對這些規(guī)則的實(shí)現(xiàn) Low level modules: implementation of the business rules

    ? 高層模塊完全依賴調(diào)用低層模塊提供的功能來完成自己的功能

      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 ?
  比較:過程化程序與 面向?qū)ο蠹軜?gòu)
    過程化程序架構(gòu)
    面向?qū)ο笙到y(tǒng)架構(gòu) Object-Oriented Architecture
7.3 依賴倒置原則的啟發(fā)1
  ? 使用繼承,避免類之間的直接綁定 Use inheritance to avoid direct bindings to classes:
    面向接口設(shè)計(jì),而不是面向?qū)崿F(xiàn)設(shè)計(jì) Design to an interface, not an implementation!
  ? 為什么面向接口設(shè)計(jì) Design to an Interface

    ? 因?yàn)?

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

      ? 抽象概念容納的范圍廣,易于擴(kuò)展/修改 abstractions are ‘hinge points’ where it is easier to extend/modify

      ? 不應(yīng)當(dāng)修改代表抽象的類/接口,符合OCP原則 shouldn’t have to modify classes/interfaces that represent the abstraction (OCP)

      ? 舉例:中央的政策不能輕易修改,而鄉(xiāng)鎮(zhèn)的政策,錯(cuò)了馬上改

    ? 例外情況 Exceptions

      ? 有些類非常成熟、穩(wěn)定 Some classes are very unlikely to change

        ? 插入抽象層,好處不多了,例如 String class,這里就可以直接使用具體類

        ? 此時(shí),不考慮依賴倒置的問題了
    避免傳遞性依賴 Avoid Transitive Dependencies
    ? 使用繼承機(jī)制,消除傳遞性依賴
    ? 如果對自己設(shè)計(jì)的類找不到一個(gè)滿意的解決方案,嘗試把職責(zé)委 派其他一個(gè)或者多個(gè)類

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

    每當(dāng)有疑慮時(shí),增加一個(gè)間接層 When in doubt, add a level of indirection

轉(zhuǎn)載于:https://www.cnblogs.com/mayZhou/p/10550064.html

總結(jié)

以上是生活随笔為你收集整理的7.12 其他面向对象设计原则3: 依赖倒置原则DIP的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。