溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

基于Spring(Boot)下策略模式的使用方法教程

發(fā)布時(shí)間:2021-10-18 13:33:51 來源:億速云 閱讀:247 作者:iii 欄目:web開發(fā)

這篇文章主要講解了“基于Spring(Boot)下策略模式的使用方法教程”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“基于Spring(Boot)下策略模式的使用方法教程”吧!

未使用策略模式時(shí)的處理

以物聯(lián)網(wǎng)為例大家可能不夠熟悉,下面就以支付場(chǎng)景為例。比如在支付的過程中我們可能會(huì)選擇微信支付、支付寶支付或銀卡支付。同時(shí),銀行卡又分不同銀行,這里統(tǒng)一為銀行卡。

最簡單直接的代碼實(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è)原則:單一職責(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í)例化。

用類圖來表示(省略策略工廠類)如下圖:

基于Spring(Boot)下策略模式的使用方法教程

image

基于Spring的策略模式實(shí)現(xiàn)

目前在實(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上下文角色,也叫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ìn)方案

通過上面的代碼基本上已經(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í)例注入為List。      */     @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)一步改造,程序變得更加靈活了。

感謝各位的閱讀,以上就是“基于Spring(Boot)下策略模式的使用方法教程”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)基于Spring(Boot)下策略模式的使用方法教程這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI