在Spring boot項目中使用策略模式消除if-else


在Spring boot項目中使用策略模式消除if-else代碼

在實際的項目中,隨着場景的增加,代碼的邏輯會越來越復雜。此前苦於代碼中if...else越來越多,所以運用了設計模式中的策略模式對其進行重構。

業務場景概述


1. 業務為扣款業務
2. 因為扣款通道的不同,需要對其進行判斷,從而調用不同的扣款接口
3. 項目開始,並沒有太多的扣款通道,所以使用了簡單的 if  else結構,但是隨着項目的日益龐大,這種結構愈發臃腫

使用策略模式進行初步重構

在學習設計模式---策略模式時,常看的說,策略模式可以對系統中的if else結構進行重構,但是.....

一般是這樣的:


@Service
@Slf4j
public class PayEngineServiceImpl implements  PayEngineService {
    if{
        // 邏輯1
        ......
    } else {
        // 邏輯2
        ......
    }
}

到這種


@Service
@Slf4j
public class PayEngineServiceImpl implements  PayEngineService {

    @Resource
    protected OrderPayService normalPayServiceImpl;

    @Resource
    protected OrderPayService discountPayServiceImpl;

    if(conditions){
        // 邏輯1
        normalPayServiceImpl.orderPay();
    } else {
        // 邏輯2
        discountPayServiceImpl.orderPay();
    }
}

@Service
@Slf4j
public class NormalPayServiceImpl implements  OrderPayService {

    @Overrivde
    public void orderPay(){
        // 邏輯1
        ......
    }
}

@Service
@Slf4j
public class DiscountPayServiceImpl implements  OrderPayService {

    @Overrivde
    public void orderPay(){
        // 邏輯2
        ......
    }
}

也不能說沒有進行優化,但是這種依然不能避免 if else 結構

結合Spring對其進行重構

業務調用方法


@Service
@Slf4j
public class PayEngineServiceImpl implements  PayEngineService {

    public void call(String deductChannel){
       OrderPayService orderPayService = PayServiceFactory.getPayEngineService(deductChannel);
       orderPayService.orderPay();
    }
}

具體實現類


@Service
@Slf4j
public class NormalPayServiceImpl implements  OrderPayService, InitializingBean {

    @Overrivde
    public void orderPay(){
        // 邏輯1
        ......
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        PayServiceFactory.registerPayService("2", this);
    }
}

@Service
@Slf4j
public class DiscountPayServiceImpl implements  OrderPayService, InitializingBean {

    @Overrivde
    public void orderPay(){
        // 邏輯2
        ......
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        PayServiceFactory.registerPayService("1", this);
    }
}

工廠方法,用於存儲獲取 OrderPayService


/**
 * 支付服務的工廠類
 *
 * @author leo
 */
public class PayServiceFactory {

    private static Map<String, PayEngineService> payServiceMap = new ConcurrentHashMap<>(8);

    public static PayEngineService getPayEngineService(String deductChannel){
        return payServiceMap.get(deductChannel);
    }

    public static void registerPayService(String deductChannel, PayEngineService service) {
        payServiceMap.put(deductChannel, service);
    }

}

再進一步重構

code ---> refactor ---> code 是編碼中一直進行的。在這種項目中,可以將各個實現類中的共有方法抽取到一個抽象層中,以對代碼進一步重構,設計模式中較模版方法

以下為簡略的介紹

抽象層:


@Slf4j
public abstract class BasePayService implements PayService, InitializingBean {
    // 共有方法
    protected void getCommonParam() {
        // 獲取公共參數
        .....    
    }

}

具體實現類


@Service
@Slf4j
public class NormalPayServiceImpl extends BasePayService {

    @Overrivde
    public void orderPay(){
        // 邏輯1
        getCommonParam();
        ......
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        PayServiceFactory.registerPayService("2", this);
    }
}


結合Spring框架,策略模式,工廠模式以及模版方法,可以避免在項目中大規模的使用if else結構,減少重復代碼。

PS: 設計模式的思想更重要,很多巨作的思想更重要,但是運用到實際較難


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM