C#面向?qū)ο缶幊讨幸蕾嚪崔D(zhuǎn)原則的示例詳解
在面向?qū)ο缶幊讨校?strong>SOLID 是五個設(shè)計原則的首字母縮寫,旨在使軟件設(shè)計更易于理解、靈活和可維護(hù)。這些原則是由美國軟件工程師和講師羅伯特·C·馬丁(Robert Cecil Martin)提出的許多原則的子集,在他2000年的論文《設(shè)計原則與設(shè)計模式》中首次提出。
SOLID 原則包含:
- S:單一功能原則(single-responsibility principle)
- O:開閉原則(open-closed principle)
- L:里氏替換原則(Liskov substitution principle)
- I:接口隔離原則(Interface segregation principle)
- D:依賴反轉(zhuǎn)原則(Dependency inversion principle)
本文我們來介紹依賴反轉(zhuǎn)原則。
依賴反轉(zhuǎn)原則
在面向?qū)ο缶幊填I(lǐng)域中,依賴反轉(zhuǎn)原則(Dependency inversion principle,DIP)是指一種特定的解耦形式,使得高層次的模塊不依賴于低層次模塊的實(shí)現(xiàn)細(xì)節(jié),依賴關(guān)系被顛倒(反轉(zhuǎn)),從而使低層次模塊依賴于高層次模塊的需求抽象。(傳統(tǒng)的依賴關(guān)系創(chuàng)建在高層次上,而具體的策略設(shè)置則應(yīng)用在低層次的模塊上)
(圖1 中,高層 對象A 依賴于低層 對象B 的實(shí)現(xiàn);圖2 中,把高層 對象A 對低層對象的需求抽象為一個 接口A,低層 對象B 實(shí)現(xiàn)了 接口A,這就是依賴反轉(zhuǎn)。)
依賴反轉(zhuǎn)原則約定:
- 高層次的模塊不應(yīng)該依賴于低層次的模塊,兩者都應(yīng)該依賴于抽象接口。
- 抽象接口不應(yīng)該依賴于具體實(shí)現(xiàn)。而具體實(shí)現(xiàn)則應(yīng)該依賴于抽象接口。
該原則顛倒了一部分人對于面向?qū)ο笤O(shè)計的認(rèn)識方式(如高層次和低層次對象都應(yīng)該依賴于相同的抽象接口)。
依賴注入是該原則的一種實(shí)現(xiàn)方式。
C# 示例
先定義一個商品信息類:
public class ProductInfo { public int ID { get; set; } public string ProductName { get; set; } public string ProductSpec { get; set; } public int Stock { get; set; } }
糟糕的示范
新建一個數(shù)據(jù)訪問類 ProductDataAccess 和業(yè)務(wù)邏輯類 ProductBusinessLogic:
public class ProductDataAccess { public ProductInfo GetDetail(int id) { ProductInfo product = new() { ID = id, ProductName = "白糖", ProductSpec = "500g", Stock = 100 }; return product; } } public class ProductBusinessLogic { private readonly ProductDataAccess _productDataAccess; public ProductBusinessLogic() { _productDataAccess = new ProductDataAccess(); } public ProductInfo GetProductDetails(int id) { return _productDataAccess.GetDetail(id); } }
在上面的代碼中,高層次的類 ProductBusinessLogic 直接依賴于低層次的類 ProductDataAccess,這明顯違反了 依賴反轉(zhuǎn)原則。
正確的示范
根據(jù) 依賴反轉(zhuǎn)原則 的要求,我們把高層對象 ProductBusinessLogic 對低層對象的需求抽象為一個接口 IProductDataAccess:
public interface IProductDataAccess { ProductInfo GetDetail(int id); }
在低層對象 ProductDataAccess 中實(shí)現(xiàn)接口 IProductDataAccess,然后在高層對象 ProductBusinessLogic 中引用(注入)接口 IProductDataAccess:
public class ProductDataAccess : IProductDataAccess { public ProductInfo GetDetail(int id) { ProductInfo product = new() { ID = id, ProductName = "白糖", ProductSpec = "500g", Stock = 100 }; return product; } } public class ProductBusinessLogic { private readonly IProductDataAccess _productDataAccess; public ProductBusinessLogic(IProductDataAccess productDataAccess) { _productDataAccess = productDataAccess; } public ProductInfo GetProductDetails(int id) { return _productDataAccess.GetDetail(id); } }
這樣,這些類的設(shè)計便遵守了依賴反轉(zhuǎn)原則。
其實(shí),ASP.NET Core 中服務(wù)的依賴注入正是遵循了依賴反轉(zhuǎn)原則。
總結(jié)
本文我介紹了 SOLID 原則中的依賴反轉(zhuǎn)原則(Dependency inversion principle),并通過 C# 代碼示例簡明地詮釋了它的含意和實(shí)現(xiàn),希望對您有所幫助。
到此這篇關(guān)于C#面向?qū)ο缶幊讨幸蕾嚪崔D(zhuǎn)原則的示例詳解的文章就介紹到這了,更多相關(guān)C#依賴反轉(zhuǎn)原則內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Unity實(shí)現(xiàn)局域網(wǎng)聊天室功能
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)局域網(wǎng)聊天室功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-10-10C#中Winform獲取文件路徑的方法實(shí)例小結(jié)
這篇文章主要介紹了C#中Winform獲取文件路徑的方法,以實(shí)例形式較為詳細(xì)的總結(jié)了WinForm關(guān)于路徑操作的常用技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-10-10詳解C#開發(fā)Android應(yīng)用程序的流程
在本篇文章里小編給大家分享了關(guān)于C#開發(fā)Android應(yīng)用程序的流程和相關(guān)技巧,需要的朋友們跟著學(xué)習(xí)下。2019-03-03C#并發(fā)容器之ConcurrentDictionary與普通Dictionary帶鎖性能詳解
這篇文章主要介紹了C#并發(fā)容器之ConcurrentDictionary與普通Dictionary帶鎖性能詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-04-04C#中使用Lambda表達(dá)式自定義比較器實(shí)現(xiàn)兩個列表合并實(shí)例
這篇文章主要介紹了C#中使用Lambda表達(dá)式自定義比較器實(shí)現(xiàn)兩個列表的合并實(shí)例,本文給出示例代碼和運(yùn)行效果,需要的朋友可以參考下2014-10-10