這篇文章主要介紹“Spring Boot策略模式怎么使用”,在日常操作中,相信很多人在Spring Boot策略模式怎么使用問題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”Spring Boot策略模式怎么使用”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!
10年積累的成都網(wǎng)站制作、網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶對(duì)網(wǎng)站的新想法和需求。提供各種問題對(duì)應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先網(wǎng)站設(shè)計(jì)后付款的網(wǎng)站建設(shè)流程,更有鎮(zhèn)賚免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
以物聯(lián)網(wǎng)為例大家可能不夠熟悉,下面就以支付場(chǎng)景為例。比如在支付的過程中我們可能會(huì)選擇微信支付、支付寶支付或銀卡支付。同時(shí),銀行卡又分不同銀行,這里統(tǒng)一為銀行卡。
最簡(jiǎn)單直接的代碼實(shí)現(xiàn)形式如下:
public void pay(String payType){ if("alipay".equals(payType)){ System.out.println("支付寶"); }else if("wechatPay".equals(payType)){ System.out.println("微信支付"); } else if("bank".equals(payType)){ System.out.println("銀行卡支付"); } }
這樣對(duì)照設(shè)計(jì)模式,通常不符合兩個(gè)原則:?jiǎn)我宦氊?zé)原則和開閉原則。
我們會(huì)發(fā)現(xiàn)當(dāng)前類(或方法)不處理了多個(gè)業(yè)務(wù)的功能,一旦任何一個(gè)支付方式的修改都可能會(huì)影響到其他的支付方式。同時(shí),無法做到對(duì)擴(kuò)展開放,對(duì)修改關(guān)閉。新增其他支付方式時(shí)同樣要修改ifelse判斷,影響到其他的業(yè)務(wù)邏輯。
而策略模式通常就是解決這種有很多ifelse處理邏輯,從而提高代碼的可維護(hù)性、可擴(kuò)展性和可讀性。
在對(duì)上述代碼進(jìn)行改造之前,先來了解一下策略模式的基本組成。
策略模式(Strategy),定義了一組算法,將每個(gè)算法都封裝起來,并且使它們之間可以互換。
策略模式通常有以下幾部分組成:
Strategy策略類,用于定義所有支持算法的公共接口;
ConcreteStrategy具體策略類,封裝了具體的算法或行為,繼承于Strategy。
Context上下文,用一個(gè)ConcreteStrategy來配置,維護(hù)一個(gè)對(duì)Strategy對(duì)象的引用;
StrategyFactory策略工廠類,用于創(chuàng)建策略類的具體實(shí)現(xiàn);通常此部分可省略,看具體情況。比如后續(xù)實(shí)例中通過Spring的依賴注入機(jī)制實(shí)現(xiàn)了策略類的實(shí)例化。
用類圖來表示(省略策略工廠類)如下圖:
目前在實(shí)踐中通常都是基于Spring的特性來實(shí)現(xiàn)策略模式,這里就以此為例來進(jìn)行講解。
上面已經(jīng)提到,策略類用于定義功能的接口,對(duì)于支付場(chǎng)景則可命名為PaymentService或PaymentStrategy。
public interface PaymentService { /** * 支付 */ PayResult pay(Order order); }
同時(shí)提供該策略類的不同實(shí)現(xiàn)類:AlipayService、WeChatPayService、BankPayService。
@Service("alipay") public class AlipayService implements PaymentService { @Override public PayResult pay(Order order) { System.out.println("Alipay"); return null; } }
@Service("wechatPay") public class WeChatPayService implements PaymentService { @Override public PayResult pay(Order order) { System.out.println("WeChatPay"); return null; } }
@Service("bank") public class BankPayService implements PaymentService { @Override public PayResult pay(Order order) { System.out.println("BankPay"); return null; } }
具體實(shí)現(xiàn)的實(shí)例化,可以通過一個(gè)PaymentFactory來進(jìn)行構(gòu)建存儲(chǔ),也可以直接利用@Autowired形式注入到Context的List或Map當(dāng)中。
PaymentFactory的實(shí)現(xiàn)如下:
public class PaymentFactory { private static final Map<String, PaymentService> payStrategies = new HashMap<>(); static { payStrategies.put("alipay", new AlipayService()); payStrategies.put("wechatPay", new WeChatPayService()); payStrategies.put("bank", new BankPayService()); } public static PaymentService getPayment(String payType) { if (payType == null) { throw new IllegalArgumentException("pay type is empty."); } if (!payStrategies.containsKey(payType)) { throw new IllegalArgumentException("pay type not supported."); } return payStrategies.get(payType); } }
通過static靜態(tài)代碼塊來初始化對(duì)應(yīng)的策略實(shí)現(xiàn)類,然后提供一個(gè)getPayment方法,根據(jù)支付類型來獲取對(duì)應(yīng)的服務(wù)。當(dāng)然,通過static初始化的代碼塊是單例的無狀態(tài)的,如果需要有狀態(tài)的類則getPayment方法,每次都需要new一個(gè)新的對(duì)象。
public static PaymentService getPayment1(String payType) { if (payType == null) { throw new IllegalArgumentException("pay type is empty."); } if ("alipay".equals(payType)) { return new AlipayService(); } else if ("wechatPay".equals(payType)) { return new WeChatPayService(); } else if ("bank".equals(payType)) { return new BankPayService(); } throw new IllegalArgumentException("pay type not supported."); }
Context上下文角色,也叫Context封裝角色,起承上啟下的作用,屏蔽高層模塊對(duì)策略、算法的直接訪問,封裝可能存在的變化。
上面通過工廠的形式創(chuàng)建策略類的實(shí)現(xiàn)類,當(dāng)然也可以直接通過@Autowired注入到Context上下文中。
@Component public class PaymentStrategy { @Autowired private final Map<String, PaymentService> payStrategies = new HashMap<>(); public PaymentService getPayment(String payType) { if (payType == null) { throw new IllegalArgumentException("pay type is empty."); } if (!payStrategies.containsKey(payType)) { throw new IllegalArgumentException("pay type not supported."); } return payStrategies.get(payType); } }
上面通過@Autowired注解,將通過@Service實(shí)例化的PaymentService實(shí)現(xiàn)類,注入到map當(dāng)中,其中key為實(shí)例化類的名稱,value為具體的實(shí)例化類。
上面的getPayment代碼與PaymentFactory中一致。當(dāng)然,還可以在PaymentStrategy中封裝一個(gè)pay方法,這樣,客戶端直接注入PaymentStrategy類調(diào)用pay方法即可。
public PayResult pay(String payType,Order order){ PaymentService paymentService = this.getPayment(payType); return paymentService.pay(order); }
通過上面的代碼基本上已經(jīng)實(shí)現(xiàn)了策略模式,此時(shí)當(dāng)新增加一個(gè)支付通道時(shí),已經(jīng)不用修改PaymentStrategy相關(guān)的代碼,只用新增一個(gè)實(shí)現(xiàn)PaymentService接口的類即可。
但在接口定義這里,還是有優(yōu)化空間的。比如,這里判斷是通過Bean的名稱來判斷的,但某些情況下判斷可能比較復(fù)雜或可能會(huì)同時(shí)執(zhí)行多個(gè)Service。此時(shí),就可以對(duì)PaymentService接口進(jìn)行改進(jìn),新增一個(gè)檢驗(yàn)是否支持該功能的判斷方法。
public interface PaymentService { boolean isSupport(Order order); /** * 支付 */ PayResult pay(Order order); }
由實(shí)現(xiàn)類來具體實(shí)現(xiàn)isSupport方法,判斷自己支持哪些功能。
同時(shí),上下文類也可以進(jìn)一步利用Java8提供的Steam特性進(jìn)行處理:
@Component public class PaymentStrategy { /** * 此處用@Autowired將所有實(shí)例注入為L(zhǎng)ist。 */ @Autowired private List<PaymentService> paymentServices; public void pay(Order order) { PaymentService paymentService = paymentServices.stream() .filter((service) -> service.isSupport(order)) .findFirst() .orElse(null); if (paymentService != null) { paymentService.pay(order); } else { throw new IllegalArgumentException("pay type not supported."); } } }
通過進(jìn)一步改造,程序變得更加靈活了。
到此,關(guān)于“Spring Boot策略模式怎么使用”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
網(wǎng)頁名稱:SpringBoot策略模式怎么使用
網(wǎng)站地址:http://sd-ha.com/article40/jsjcho.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器、微信小程序、網(wǎng)站制作、網(wǎng)站策劃、網(wǎng)站設(shè)計(jì)公司、搜索引擎優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)