支付业务实战对复杂if else 判断的优化

简介: 支付业务实战对复杂if else 判断的优化

背景

最近在做项目的时候,需要接入支付。由于接入第三方支付而且还不知止一家,需要接入很多家。比如说支付宝、微信、富友支付等。每家支付都一个回调。现如今的代码,根据不同的第三方支付一大堆else if判断。现如今代码如下:

 public PayResponse pay(PayRequestType payRequestType) {
        PayTypeEnum payType = PayTypeEnum.para(payRequestType.getPayType());
        if (payType == PayTypeEnum.ALIPAY) {
            return alipayService.pay(payRequestType);
        } else if (payType == PayTypeEnum.WEIXIN) {
            return weixinPayService.pay(payRequestType);
        } else if (payType == PayTypeEnum.LIANLIAN) {
            return lianlianPayService.pay(payRequestType);
        }
        // 其他支付方式
        return null;
    }

如果以后要接入其他的支付方式,然后就要接着else if 往下写,如果十几家怎么办?所以这个要进行优化。

优化步骤

  1. 创建一个支付接口,提供两个方法 <br/>
public interface Pay {

    PayResponse pay(PayRequestType payRequestType);

    /**
     * 每家支付方式对应的类型
     * @return
     */
    PayTypeEnum getPayType();
}```
每家支付都去实现这个类:比如微信支付

@Component
public class WeixinPayService implements Pay {

@Override
public PayResponse pay(PayRequestType payRequestType) {
    return null;
}

@Override
public PayTypeEnum getPayType() {
    return PayTypeEnum.WEIXIN;
}
然后准备一个工厂把那些判断if else 消除掉

public final class PayFactory {

private PayFactory() {
}
public static Map<PayTypeEnum, Pay> PAYMAP = new ConcurrentHashMap();
static {
    Map<String, Pay> beansOfType = ApplicationContextHelper.getBeansOfType(Pay.class);
    for (Map.Entry<String, Pay> entry : beansOfType.entrySet()) {
        Pay pay = entry.getValue();
        PAYMAP.put(pay.getPayType(), pay);
    }
}

public static Pay getPay(PayTypeEnum payTypeEnum){
    return  PAYMAP.get(payTypeEnum);
}
spring获取bean帮助类

@Component
public class ApplicationContextHelper implements ApplicationContextAware {

public static ApplicationContext applicationContext;

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    this.applicationContext = applicationContext;
}
public static  <T> T getBean(Class<T> clazz) {
    return applicationContext.getBean(clazz);
}

public static <T> Map<String, T> getBeansOfType(Class<T> clazz) {
    return applicationContext.getBeansOfType(clazz);
}
## 优化后代码
类型枚举新增一个枚举转换方法

public enum PayTypeEnum {

WEIXIN, LIANLIAN, ALIPAY;

public static PayTypeEnum para(String type){

    for(PayTypeEnum payTypeEnum:PayTypeEnum.values()){
        if(type.equalsIgnoreCase(payTypeEnum.name())){
            return  payTypeEnum;
        }
    }
    return null;
}

}

public PayResponse pay2(PayRequestType payRequestType) {

    PayTypeEnum payType = PayTypeEnum.para(payRequestType.getPayType());
   return PayFactory.getPay(payType).pay(payRequestType);
}
后续新增支付方式的话,只要新增枚举类型、然后实现pay接口就可以了。没有了复杂的if else 判断了。
##  总结
目录
相关文章
|
2月前
|
安全
外汇交易所系统开发规则玩法/步骤逻辑/方案项目/教程指南/源码流程
The development of foreign exchange system involves a series of functions and features, aiming to provide a safe, efficient, transparent, and reliable trading platform for foreign exchange trading. The following are the functions that may be involved in the development of the foreign exchange exchan
|
3月前
|
存储 人工智能 分布式计算
swap去中心化交易系统开发|详情规则|设计原理
Web3.0的技术支撑包括区块链技术、人工智能、物联网、去中心化存储、加密算法、分布式计算等。
游戏对接广告看视频系统开发详细规则/方案逻辑/步骤逻辑/规则玩法/源码程序
Advertising location and display method: According to the characteristics of the game interface and scene, choose the appropriate advertising location and display method to ensure that the advertisement naturally integrates into the game and does not affect the player&#39;s game experience.
|
4月前
|
安全
外贸订单管理的管理要点:流程、准确性、跟进、合理安排资源
在外贸公司的订单管理中,建立订单管理流程是至关重要的。一个完善的流程可以帮助公司更好地管理订单,提高工作效率,确保订单的准确性和及时交付。
106 2
|
9月前
|
新零售 人工智能 供应链
链动2+1开发运营版丨链动2+1系统开发案例详情/方案项目/功能说明/逻辑规则/源码程序
新零售是指传统零售业与互联网技术相结合,通过数字化、数据化和智能化的手段,重新定义和升级传统零售业态的模式和经营方式。
|
5月前
|
存储 搜索推荐 API
拼多多ID取商品详情API:电商行业的核心价值与实时数据获取策略
在当今的电商行业中,数据是驱动业务决策和优化用户体验的关键因素。拼多多作为中国电商市场的主要参与者,其根据ID取商品详情原数据的API在电商行业中具有显著的重要性。本文将深入探讨这个话题,并介绍如何实现实时数据获取。
|
5月前
怎么做问答推广?问答营销的步骤和逻辑
怎么做问答推广?问答营销的步骤和逻辑
|
7月前
|
前端开发 JavaScript API
89分布式电商项目 -检测支付状态
89分布式电商项目 -检测支付状态
23 0
|
9月前
|
Java Spring
统计业务方法耗时【项目 商城】
统计业务方法耗时【项目 商城】
70 0
统计业务方法耗时【项目 商城】
|
9月前
|
安全 区块链
数字货币交易所系统开发(开发逻辑)丨案例详情丨规则玩法丨设计方案丨需求实现丨源码功能
The development process of a digital currency exchange system is an important step, which includes stages such as requirement analysis, system design, coding implementation, testing, and online deployment. In the requirements analysis stage, the development team needs to fully communicate with the