依賴倒置原則(DIP)
依賴倒置(Dependency Inversion Principle,縮寫DIP)是面向?qū)ο罅蠡驹瓌t之一。他是指一種特定的的解耦形式,使得高層次的模塊不依賴低層次的模塊的實(shí)現(xiàn)細(xì)節(jié),依賴關(guān)系被顛倒(反轉(zhuǎn)),從而使得低層次模塊依賴于高層次模塊的需求抽象.
該原則規(guī)定:
- 高層次的模塊不應(yīng)該依賴低層次模塊,二者都應(yīng)該依賴其抽象接口.
- 抽象接口不應(yīng)該依賴于具體實(shí)現(xiàn),而具體實(shí)現(xiàn)則應(yīng)該依賴于抽象接口.
通過(guò)如下一個(gè)簡(jiǎn)單的示例,我們來(lái)看一下,我們通過(guò)一個(gè)簡(jiǎn)單地下單流程向我們的用戶發(fā)送相關(guān)的短信或者郵件.
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); } } |
這樣看我們的代碼沒(méi)問(wèn)題,目前只要我們完成了訂單操作那么,那么則會(huì)觸發(fā)發(fā)送功能,但是他卻違反了DIP,因?yàn)镺rdering類依賴于SendingEmail類,而SendingEmail類不是抽象類,而是一個(gè)具體的類.那我們?cè)賮?lái)想一個(gè)如果這時(shí)候業(yè)務(wù)口的人過(guò)來(lái)向我們提出了一個(gè)新的需求,要求我們改為短信而不是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); } } } |
根據(jù)上述需求我們不得不創(chuàng)建更多的類,并且在Ordering類中聲明他,最后我們還需要使用IF ELSE語(yǔ)句來(lái)決定使用SMS還是使用電子郵件.但是當(dāng)我們有更多這種處理操作后,那么可能比現(xiàn)在還混亂,這就意味著我們必須在Ordering類中聲明更多新的具體類的實(shí)例.
我們需要抽離出來(lái)一種方式,讓高級(jí)模塊去依賴于抽象,用它來(lái)代替我們實(shí)現(xiàn)類,該抽象將映射到實(shí)現(xiàn)類.
控制反轉(zhuǎn)(Inversion of Control,縮寫為IOC)是面向?qū)ο笾械脑O(shè)計(jì)原則,他可以幫助我們使高層模塊依賴于抽象,而不是底層模塊的具體實(shí)現(xiàn).換句話說(shuō),他有助于實(shí)現(xiàn)(依賴倒置原則——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 } } |
我們?cè)賮?lái)修改一下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); } } } |
通過(guò)如上修改我們做的控制反轉(zhuǎn)更符合DIP.現(xiàn)在我們的高級(jí)模塊只需要依賴于抽象,而不用去依賴實(shí)現(xiàn).
依賴注入(DI)
依賴注入(Depeondency Injection,縮寫為DI)是實(shí)現(xiàn)控制反轉(zhuǎn)的一種方式.常用的依賴注入方法有3種:
- 構(gòu)造函數(shù)注入
- 方法注入
- 屬性注入
雖然說(shuō)通過(guò)上面代碼我們實(shí)現(xiàn)了IoC,并且Ordering類依賴于ICustomerCommunication抽象,但我們?nèi)匀辉贠rdering類中使用了實(shí)現(xiàn)類,這使用我們無(wú)法在類于類之間完全解耦.
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); } |
那我們?cè)賮?lái)說(shuō)說(shuō)DI,DI主要幫助我們將實(shí)現(xiàn)注入到抽象的類(ICustomerCommunication接口)中.DI的主要減少類之間的耦合,并且將抽象和具體實(shí)現(xiàn)的綁定移除依賴類.
構(gòu)造函數(shù)注入
通過(guò)構(gòu)造函數(shù)注入我們將實(shí)現(xiàn)類的對(duì)象傳遞給依賴類的構(gòu)造函數(shù),并將其分配給這個(gè)接口.
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); } } |
在上面的代碼中,構(gòu)造函數(shù)將采用實(shí)現(xiàn)類對(duì)象綁定到接口中.如果我們將SendingSMS的實(shí)現(xiàn)傳遞給這個(gè)類,我們要做的就是聲明一個(gè)SendingSMS類的實(shí)例,然后將其傳遞給Ordering的構(gòu)造函數(shù),如下所示:
方法注入
通過(guò)使用構(gòu)造函數(shù)注入,我們將不得不在Ordering類的生存期內(nèi)使用實(shí)現(xiàn)類的實(shí)例SendingSMS或SendingEmail類.現(xiàn)在如果要在每次調(diào)用該方法時(shí)傳遞實(shí)現(xiàn)類的實(shí)例,則必須使用方法注入.
1
2
3
4
5
6
7
|
public class Ordering { public void Order(ICustomerCommunication customerComm, string message){ _customerComm=customerComm; _customerComm.Send(message); } } |
調(diào)用方式如下所示
1
2
3
|
SendingSMS sendingSMS= new SendingSMS(); Ordering ordering= new Ordering(sendingSMS); ordering.Order(sendingSMS, "msg" ); |
屬性注入
通過(guò)如上描述我們知道了構(gòu)造函數(shù)注入方法在整個(gè)生命周期中使用依賴類,而方法注入是將我們的注入直接去限于該方法中,然后我們?cè)偃チ私庖幌聦傩宰⑷?/p>
1
2
3
4
5
6
7
|
public class Ordering { public ICustomerCommunication customerComm { get ; set ;} public void Order( string message){ _customerComm.Send(message); } } |
調(diào)用方式如下所示
1
2
3
4
|
SendingSMS sendingSMS= new SendingSMS(); Ordering ordering= new Ordering(sendingSMS); ordering.customerComm=sendingSMS; ordering.Order( "msg" ); |
其實(shí)構(gòu)造函數(shù)注入是實(shí)現(xiàn)DI最常用的方法.如果需要在每個(gè)方法調(diào)用上傳遞不同的依賴關(guān)系,則可以使用方法注入屬性注入的使用還是比較少的.
Reference
https://zh.wikipedia.org/wiki/控制反轉(zhuǎn)
https://zh.wikipedia.org/zh-hans/依賴反轉(zhuǎn)原則
到此這篇關(guān)于.NET IoC模式依賴反轉(zhuǎn)(DIP)、控制反轉(zhuǎn)(Ioc)、依賴注入(DI)的文章就介紹到這了,更多相關(guān).NET IoC模式依賴反轉(zhuǎn)、控制反轉(zhuǎn)、依賴注入內(nèi)容請(qǐng)搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!
原文鏈接:https://www.cnblogs.com/yyfh/p/12874075.html