設計模式六大原則:依賴倒置原則


目錄: 

  設計模式六大原則:單一職責原則

  設計模式六大原則:接口隔離原則 

  設計模式六大原則:依賴倒置原則

  設計模式六大原則:里氏替換原則

  設計模式六大原則:迪米特法則

  設計模式六大原則:開閉原則

依賴倒置原則(Dependence Inversion Principle):  

  1、高層模塊不應該依賴底層模塊,二者都應該依賴抽象。

  2、抽象不應該依賴細節,細節應該依賴抽象。

  3、依賴倒置的中心思想是面向接口編程。

  4、依賴倒置原則是基於這樣的設計理念:相對於細節的多變性,抽象的東西要穩定的多。以抽象為基礎搭建的架構比以細節為基礎搭建的架構要穩定的多。

  5、使用接口或抽象類的目的是指定好規范,而不涉及任何具體的操作,把展現細節的任務交給他們的實現類來完成。

經典案例:

  三二班有個小明,想要學習C#,於是買了本《深入理解C#》進行學習。 

 1 internal class Program
 2 {
 3     private static void Main(string[] args)
 4     {
 5         XiaoMing xm = new XiaoMing();
 6         xm.Study(new CSharp());
 7     }
 8 }
 9 
10 internal class CSharp
11 {
12     public void ShowMsg()
13     {
14         Console.WriteLine("《深入理解C#》");
15     }
16 }
17 
18 internal class XiaoMing
19 {
20     public void Study(CSharp cSharp)
21     {
22         cSharp.ShowMsg();
23     }
24 }
view code

  過了一段時間,小明覺得光學習一門太沒有意思了。聽說Linux比較好玩,於是買了本《鳥哥的私房菜Linux》。

 1 internal class Program
 2 {
 3     private static void Main(string[] args)
 4     {
 5         XiaoMing xm = new XiaoMing();
 6         xm.Study(new CSharp());
 7         xm.Study(new Linux());
 8     }
 9 }
10 
11 internal class CSharp
12 {
13     public void ShowMsg()
14     {
15         Console.WriteLine("《深入理解C#》");
16     }
17 }
18 
19 internal class Linux
20 {
21     public void ShowMsg()
22     {
23         Console.WriteLine("《鳥哥的私房菜Linux》");
24     }
25 }
26 
27 internal class XiaoMing
28 {
29     public void Study(CSharp cSharp)
30     {
31         cSharp.ShowMsg();
32     }
33 
34     public void Study(Linux linux)
35     {
36         linux.ShowMsg();
37     }
38 }
view code

  小明是一個聰明的娃,過了一段時間學得差不多了,於是又想學習《設計模式》...就這樣小明在不斷學習中成長,而我們的代碼卻越來越臃腫,變得難以維護。由於XiaoMing是一個高級模塊並且是一個細節實現類,此類依賴了書籍CSharp和Linux又是一個細節依賴類,這導致XiaoMing每讀一本書都需要修改代碼,這與我們的依賴倒置原則是相悖的。那如何解決XiaoMing的這種問題呢? 

 1 internal class Program
 2 {
 3     private static void Main(string[] args)
 4     {
 5         XiaoMing xm = new XiaoMing();
 6         xm.Study(new CSharp());
 7         xm.Study(new Linux());
 8     }
 9 }
10 
11 internal interface IBook
12 {
13     void ShowMsg();
14 }
15 
16 internal class CSharp : IBook
17 {
18     public void ShowMsg()
19     {
20         Console.WriteLine("《深入理解C#》");
21     }
22 }
23 
24 internal class Linux : IBook
25 {
26     public void ShowMsg()
27     {
28         Console.WriteLine("《鳥哥的私房菜Linux》");
29     }
30 }
31 
32 internal class XiaoMing
33 {
34     public void Study(IBook book)
35     {
36         book.ShowMsg();
37     }
38 }
view code

  我們發現,只要讓XiaoMing依賴於抽象IBook,其他書籍依賴於該抽象,以后不管小明讀什么書,哈哈都是so easy的。

依賴關系傳遞的三種方式:

  1、通過接口傳遞(上述示例) 

1 internal class XiaoMing
2 {
3     // 2.通過接口傳遞依賴對象
4     public void Study(IBook book)
5     {
6         book.ShowMsg();
7     }
8 }
view code

  2、通過構造方法傳遞  

 1 internal class XiaoMing
 2 {
 3     private IBook _book;
 4 
 5     // 1.通過構造函數傳遞依賴對象
 6     public XiaoMing(IBook book)
 7     {
 8         this._book = book;
 9     }
10 
11     public void Study()
12     {
13         this._book.ShowMsg();
14     }
15 }
view code

  3、通過Setter方法傳遞  

 1 internal class XiaoMing
 2 {
 3     private IBook _book;
 4 
 5     // 3.通過Setter方法傳遞依賴對象
 6     public void setBook(IBook _book)
 7     {
 8         this._book = _book;
 9     }
10 
11     public void Study()
12     {
13         this._book.ShowMsg();
14     }
15 }
view code

依賴倒置原則的本質就是通過抽象(接口或抽象類)使各個類或模塊的實現彼此獨立,不互相影響,實現模塊間的松耦合。我們在項目中使用這個原則要遵循下面的規則:

  1、每個類盡量都有接口或者抽象類,或者抽象類和接口兩都具備

  2、變量的表面類型盡量是接口或者抽象類

  3、任何類都不應該從具體類派生

  4、盡量不要覆寫基類的方法

  5、如果基類是一個抽象類,而這個方法已經實現了,子類盡量不要覆寫。類間依賴的是抽象,覆寫了抽象方法,對依賴的穩定性會有一定的影響

  6、結合里氏替換原則使用


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM