else 策略模式去掉if_在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 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: 设计模式的思想更重要,很多巨作的思想更重要,但是运用到实际较难

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值