我如何向团队解释依赖注入
我沒有講所有有關(guān)IOC / DI的理論,而是想舉例說明。
要求:我們將獲得一些客戶地址,并且需要驗(yàn)證該地址。
經(jīng)過一番評(píng)估,我們考慮使用Google地址驗(yàn)證服務(wù)。
傳統(tǒng)(不良)方法:
只需創(chuàng)建一個(gè)AddressVerificationService類并實(shí)現(xiàn)邏輯即可。
假設(shè)GoogleAddressVerificationService是Google提供的一項(xiàng)服務(wù),該服務(wù)將Address作為字符串并返回經(jīng)度/緯度。
class AddressVerificationService {public String validateAddress(String address){GoogleAddressVerificationService gavs = new GoogleAddressVerificationService();String result = gavs.validateAddress(address); return result;} }這種方法的問題:
1.如果要更改地址驗(yàn)證服務(wù)提供商,則需要更改邏輯。
2.您不能使用某些虛擬AddressVerificationService進(jìn)行單元測(cè)試(使用模擬對(duì)象)
由于某些原因,客戶要求我們支持多個(gè)AddressVerificationService Providers,因此我們需要確定在運(yùn)行時(shí)使用哪種服務(wù)。
為了適應(yīng)這一點(diǎn),您可能會(huì)想到更改以下類:
class AddressVerificationService { //This method validates the given address and return longitude/latitude details.public String validateAddress(String address){String result = null;int serviceCode = 2; // read this code value from a config fileif(serviceCode == 1){GoogleAddressVerificationService googleAVS = new GoogleAddressVerificationService();result = googleAVS.validateAddress(address);} else if(serviceCode == 2){YahooAddressVerificationService yahooAVS = new YahooAddressVerificationService();result = yahooAVS.validateAddress(address);}return result;} } 這種方法的問題:
?
1.每當(dāng)您需要支持新的服務(wù)提供商時(shí),都需要使用if-else-if添加/更改邏輯。 2.您不能使用某些虛擬AddressVerificationService進(jìn)行單元測(cè)試(使用模擬對(duì)象)
IOC / DI方法:
在上述方法中,AddressVerificationService負(fù)責(zé)控制其依賴項(xiàng)的創(chuàng)建。
因此,只要其依賴項(xiàng)發(fā)生更改,AddressVerificationService就會(huì)更改。
現(xiàn)在,讓我們使用IOC / DI模式重寫AddressVerificationService。
class AddressVerificationService{private AddressVerificationServiceProvider serviceProvider;public AddressVerificationService(AddressVerificationServiceProvider serviceProvider) {this.serviceProvider = serviceProvider;}public String validateAddress(String address){return this.serviceProvider.validateAddress(address);}}interface AddressVerificationServiceProvider{public String validateAddress(String address);}在這里,我們注入了AddressVerificationService依賴項(xiàng)AddressVerificationServiceProvider。
現(xiàn)在,讓我們使用多個(gè)提供程序服務(wù)來實(shí)現(xiàn)AddressVerificationServiceProvider。
class YahooAVS implements AddressVerificationServiceProvider{@Overridepublic String validateAddress(String address) {System.out.println("Verifying address using YAHOO AddressVerificationService");return yahooAVSAPI.validate(address);} }class GoogleAVS implements AddressVerificationServiceProvider{@Overridepublic String validateAddress(String address) {System.out.println("Verifying address using Google AddressVerificationService");return googleAVSAPI.validate(address);}}現(xiàn)在,客戶可以選擇使用哪個(gè)服務(wù)提供商的服務(wù),如下所示:
AddressVerificationService verificationService = null;AddressVerificationServiceProvider provider = null;provider = new YahooAVS();//to use YAHOO AVSprovider = new GoogleAVS();//to use Google AVSverificationService = new AddressVerificationService(provider);String lnl = verificationService.validateAddress("HitechCity, Hyderabad");System.out.println(lnl);對(duì)于單元測(cè)試,我們可以實(shí)現(xiàn)一個(gè)Mock AddressVerificationServiceProvider。
class MockAVS implements AddressVerificationServiceProvider{@Overridepublic String validateAddress(String address) {System.out.println("Verifying address using MOCK AddressVerificationService");return "<response><longitude>123</longitude><latitude>4567</latitude>";}}AddressVerificationServiceProvider provider = null;provider = new MockAVS();//to use MOCK AVS AddressVerificationServiceIOC verificationService = new AddressVerificationServiceIOC(provider);String lnl = verificationService.validateAddress("Somajiguda, Hyderabad");System.out.println(lnl); 通過這種方法,我們可以解決上述基于非IOC / DI的方法的問題。
1.我們可以根據(jù)需要提供盡可能多的商品。 只需實(shí)現(xiàn)AddressVerificationServiceProvider并將其注入即可。 2.我們可以使用模擬實(shí)現(xiàn)使用虛擬數(shù)據(jù)進(jìn)行單元測(cè)試。
因此,通過遵循“依賴注入”原理,我們可以創(chuàng)建基于接口的松散耦合且易于測(cè)試的服務(wù)。
參考: 我是如何通過JCG合作伙伴 Siva Reddy在“ 我的技術(shù)實(shí)驗(yàn)”博客上 向我的團(tuán)隊(duì)解釋依賴注入的 。
翻譯自: https://www.javacodegeeks.com/2012/06/how-i-explained-dependency-injection-to.html
總結(jié)
以上是生活随笔為你收集整理的我如何向团队解释依赖注入的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用Cloud SQL的Google A
- 下一篇: 编写Eclipse插件教程–第1部分