依賴倒置原則(DIP)
依賴倒置(Dependency Inversion Principle,縮寫DIP)是面向對象六大基本原則之一。他是指一種特定的的解耦形式,使得高層次的模塊不依賴低層次的模塊的實現細節,依賴關系被顛倒(反轉),從而使得低層次模塊依賴于高層次模塊的需求抽象.
該原則規定:
- 高層次的模塊不應該依賴低層次模塊,二者都應該依賴其抽象接口.
- 抽象接口不應該依賴于具體實現,而具體實現則應該依賴于抽象接口.
通過如下一個簡單的示例,我們來看一下,我們通過一個簡單地下單流程向我們的用戶發送相關的短信或者郵件.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
public SendingEmail { public void Send( string message){ //do something } } public Ordering { SendingEmail _sendingEmail= null ; public void Order( string message){ //Order business operation if (_sendingEmail == null ) { _sendingEmail= new SendingEmail(); } _sendingEmail.Send(message); } } |
這樣看我們的代碼沒問題,目前只要我們完成了訂單操作那么,那么則會觸發發送功能,但是他卻違反了DIP,因為Ordering類依賴于SendingEmail類,而SendingEmail類不是抽象類,而是一個具體的類.那我們再來想一個如果這時候業務口的人過來向我們提出了一個新的需求,要求我們改為短信而不是Email,那么我們需要怎么改?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
public class SendingSMS { public void Send( string message){ //do something } } public Ordering { SendingEmail _sendingEmail= null ; SendingSMS _sendingSMS= null ; bool isSendingSMS= true ; public void Order( string message){ //Order business operation if (isSendingSMS){ if (_sendingSMS == null ) { _sendingSMS= new SendingSMS(); } _sendingSMS.Send(message); } else { if (_sendingEmail == null ) { _sendingEmail= new SendingEmail(); } _sendingEmail.Send(message); } } } |
根據上述需求我們不得不創建更多的類,并且在Ordering類中聲明他,最后我們還需要使用IF ELSE語句來決定使用SMS還是使用電子郵件.但是當我們有更多這種處理操作后,那么可能比現在還混亂,這就意味著我們必須在Ordering類中聲明更多新的具體類的實例.
我們需要抽離出來一種方式,讓高級模塊去依賴于抽象,用它來代替我們實現類,該抽象將映射到實現類.
控制反轉(Inversion of Control,縮寫為IOC)是面向對象中的設計原則,他可以幫助我們使高層模塊依賴于抽象,而不是底層模塊的具體實現.換句話說,他有助于實現(依賴倒置原則——DIP).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
public interface ICustomerCommunication { void Send( string message); } 然后我們修改SendingEmail和SendingSMS類以從ICustomerCommunication接口繼承. public class SendingEmail:ICustomerCommunication { public void Send( string message){ //do something } } public class SendingSMS:ICustomerCommunication { public void Send( string message){ //do something } } |
我們再來修改一下Ordering類以使用該抽象接口
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
public Ordering { ICustomerCommunication _customerComm= null ; bool isSendingSMS= true ; public void Order( string message){ //Order business operation if (isSendingSMS){ if (_customerComm == null ) { _customerComm= new SendingSMS(); } _customerComm.Send(message); } else { if (_customerComm == null ) { _customerComm= new SendingEmail(); } _customerComm.Send(message); } } } |
通過如上修改我們做的控制反轉更符合DIP.現在我們的高級模塊只需要依賴于抽象,而不用去依賴實現.
依賴注入(DI)
依賴注入(Depeondency Injection,縮寫為DI)是實現控制反轉的一種方式.常用的依賴注入方法有3種:
- 構造函數注入
- 方法注入
- 屬性注入
雖然說通過上面代碼我們實現了IoC,并且Ordering類依賴于ICustomerCommunication抽象,但我們仍然在Ordering類中使用了實現類,這使用我們無法在類于類之間完全解耦.
1
2
3
4
5
6
7
8
9
10
11
12
13
|
if (isSendingSMS){ if (_customerComm == null ) { _customerComm= new SendingSMS(); } _customerComm.Send(message); } else { if (_customerComm == null ) { _customerComm= new SendingEmail(); } _customerComm.Send(message); } |
那我們再來說說DI,DI主要幫助我們將實現注入到抽象的類(ICustomerCommunication接口)中.DI的主要減少類之間的耦合,并且將抽象和具體實現的綁定移除依賴類.
構造函數注入
通過構造函數注入我們將實現類的對象傳遞給依賴類的構造函數,并將其分配給這個接口.
1
2
3
4
5
6
7
8
9
10
|
public class Ordering { ICustomerCommunication _customerComm= null ; public Ordering(ICustomerCommunication customerComm){ _customerComm=customerComm; } public void Order( string message){ _customerComm.Send(message); } } |
在上面的代碼中,構造函數將采用實現類對象綁定到接口中.如果我們將SendingSMS的實現傳遞給這個類,我們要做的就是聲明一個SendingSMS類的實例,然后將其傳遞給Ordering的構造函數,如下所示:
方法注入
通過使用構造函數注入,我們將不得不在Ordering類的生存期內使用實現類的實例SendingSMS或SendingEmail類.現在如果要在每次調用該方法時傳遞實現類的實例,則必須使用方法注入.
1
2
3
4
5
6
7
|
public class Ordering { public void Order(ICustomerCommunication customerComm, string message){ _customerComm=customerComm; _customerComm.Send(message); } } |
調用方式如下所示
1
2
3
|
SendingSMS sendingSMS= new SendingSMS(); Ordering ordering= new Ordering(sendingSMS); ordering.Order(sendingSMS, "msg" ); |
屬性注入
通過如上描述我們知道了構造函數注入方法在整個生命周期中使用依賴類,而方法注入是將我們的注入直接去限于該方法中,然后我們再去了解一下屬性注入
1
2
3
4
5
6
7
|
public class Ordering { public ICustomerCommunication customerComm { get ; set ;} public void Order( string message){ _customerComm.Send(message); } } |
調用方式如下所示
1
2
3
4
|
SendingSMS sendingSMS= new SendingSMS(); Ordering ordering= new Ordering(sendingSMS); ordering.customerComm=sendingSMS; ordering.Order( "msg" ); |
其實構造函數注入是實現DI最常用的方法.如果需要在每個方法調用上傳遞不同的依賴關系,則可以使用方法注入屬性注入的使用還是比較少的.
Reference
https://zh.wikipedia.org/wiki/控制反轉
https://zh.wikipedia.org/zh-hans/依賴反轉原則
到此這篇關于.NET IoC模式依賴反轉(DIP)、控制反轉(Ioc)、依賴注入(DI)的文章就介紹到這了,更多相關.NET IoC模式依賴反轉、控制反轉、依賴注入內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://www.cnblogs.com/yyfh/p/12874075.html