优雅解决大量if判断

优雅解决大量if判断

一:前言

大家都在项目上遇到大量的if判断影响代码的可读性和复杂度,对于后期不好维护等问题。下面如何解决大量if条件让代码看起来很优雅

二:项目需求

我在项目里有个集成支付接口,比如支付宝、微信、银联等支付接口。在前端入参的支付类型,后端根据支付类型调用封装好的支付接口,这样一来代码就会存在难以维护的if校验。

三:引入策略设计模式解决if判断

项目中的实例

1.创建一个interface接口

public interface RefundStrategy {

    /**
     *
     * @param tradeNo 交易流水号
     * @param refundNo 退款单号
     * @param totalAmount 订单总金额
     * @param refundAmount 退款金额
     * @param refundReason 退款原因
     * @return 退款结果集 {@link PayResult}
     */
    PayResult doPayRefund(String tradeNo, String refundNo, String orderNo,String thirdpartPayNo, String totalAmount, String refundAmount, String refundReason);
}

2.在Ali实现类实现该接口

Data
@Component
public class AliPayRefundStrategy implements RefundStrategy {


    private static YueProperties properties;

    private static CommonUtil commonUtil;


    private static void setCommonUtilFactory(CommonUtil util) {
        if (AliPayRefundStrategy.commonUtil == null) {
            AliPayRefundStrategy.commonUtil = util;
        }
    }

    @Resource
    public void setCommonUtil(CommonUtil util) {
        AliPayRefundStrategy.setCommonUtilFactory(util);
    }



    private static void setPropertiesFactory(YueProperties yueProperties) {
        if (AliPayRefundStrategy.properties == null) {
            AliPayRefundStrategy.properties = yueProperties;
        }
    }

    @Resource
    public void setProperties(YueProperties yueProperties) {
        AliPayRefundStrategy.setPropertiesFactory(yueProperties);
    }

    @Override
    public PayResult doPayRefund(String tradeNo, String refundNo,String orderNo,String thirdpartPayNo, String totalAmount, String refundAmount, String refundReason) {

        YuePay yuepay = new YueAliPay(properties.getPay().getAlipay().getAppId(), properties.getPay().getAlipay().getPublicKey(),
                properties.getPay().getAlipay().getPrivateKey());
        String response = yuepay.refund(tradeNo, refundNo, totalAmount, refundAmount, refundReason);
        PayResult payResult = commonUtil.readValue(response, PayResult.class);
        //获取退款交易流水号
        @SuppressWarnings("unchecked")
        Map<String, String> map = commonUtil.readValue(payResult.getParams(), Map.class);
        payResult.setTradeNo(map.get("trade_no"));
        return payResult;
    }

3.创建策略工厂

public class RefundStrategyFactory {

    private static  Map<Integer, RefundStrategy>  originalMap= new HashMap<>();
    private static  Map<Integer, RefundStrategy> unOriginalMap = new HashMap<>();
    //原路退款
    static {
        originalMap.put(ReceptionPaymentDict.RefundPayType.WXPAY_CARD.getValue(), new WxPayRefundStrategy());
        originalMap.put(ReceptionPaymentDict.RefundPayType.WXPAY_H5.getValue(), new WxPayRefundStrategy());
        originalMap.put(ReceptionPaymentDict.RefundPayType.ALIPAY.getValue(), new AliPayRefundStrategy());
        originalMap.put(ReceptionPaymentDict.RefundPayType.ALIPAY_H5.getValue(), new AliPayRefundStrategy());
        originalMap.put(ReceptionPaymentDict.RefundPayType.JHPAY.getValue(), new JuhePayRefundStrategy());
        originalMap.put(ReceptionPaymentDict.RefundPayType.WXPAY_GZH.getValue(), new WxPayRefundStrategy());
        originalMap.put(ReceptionPaymentDict.PayType.UNION_PAY.getValue(), new UnionPayRefundStrategy());
        originalMap.put(ReceptionPaymentDict.PayType.BEST_PAY.getValue(), new BestPayRefundStrategy());

    }
    //手工退款
    static {
        unOriginalMap.put(ReceptionPaymentDict.RefundPayType.JHPAY.getValue(), new OtherPayRefundStrategy());
        unOriginalMap.put(ReceptionPaymentDict.PayType.CASH.getValue(), new OtherPayRefundStrategy());
        unOriginalMap.put(ReceptionPaymentDict.RefundPayType.COMPANY_TRANSFER.getValue(), new OtherPayRefundStrategy());
        unOriginalMap.put(ReceptionPaymentDict.RefundPayType.SZ_ICBC.getValue(), new OtherPayRefundStrategy());
        unOriginalMap.put(ReceptionPaymentDict.RefundPayType.OTHER.getValue(), new OtherPayRefundStrategy());
        unOriginalMap.put(ReceptionPaymentDict.RefundPayType.ALIPAY.getValue(), new OtherPayRefundStrategy());
        unOriginalMap.put(ReceptionPaymentDict.RefundPayType.WXPAY_CARD.getValue(), new OtherPayRefundStrategy());
        unOriginalMap.put(ReceptionPaymentDict.RefundPayType.NB_ICBC.getValue(), new OtherPayRefundStrategy());
        unOriginalMap.put(ReceptionPaymentDict.RefundPayType.CQ_ICBC.getValue(), new OtherPayRefundStrategy());
        unOriginalMap.put(ReceptionPaymentDict.RefundPayType.GZ_ICBC.getValue(), new OtherPayRefundStrategy());
        unOriginalMap.put(ReceptionPaymentDict.PayType.POS.getValue(), new OtherPayRefundStrategy());
        unOriginalMap.put(ReceptionPaymentDict.PayType.BEST_PAY.getValue(), new OtherPayRefundStrategy());
        unOriginalMap.put(ReceptionPaymentDict.RefundPayType.WXPAY_CARD.getValue(), new OtherPayRefundStrategy());
        unOriginalMap.put(ReceptionPaymentDict.RefundPayType.WXPAY_H5.getValue(), new OtherPayRefundStrategy());
        unOriginalMap.put(ReceptionPaymentDict.RefundPayType.ALIPAY.getValue(), new OtherPayRefundStrategy());
        unOriginalMap.put(ReceptionPaymentDict.RefundPayType.ALIPAY_H5.getValue(), new OtherPayRefundStrategy());
        unOriginalMap.put(ReceptionPaymentDict.RefundPayType.JHPAY.getValue(), new OtherPayRefundStrategy());
        unOriginalMap.put(ReceptionPaymentDict.RefundPayType.WXPAY_GZH.getValue(), new OtherPayRefundStrategy());
        unOriginalMap.put(ReceptionPaymentDict.PayType.UNION_PAY.getValue(), new OtherPayRefundStrategy());

    }

    private RefundStrategyFactory() {

    }
   public static RefundStrategy getPayRefundStrategy(Integer key, Boolean original) {
        if (original) {
            return  originalMap.get(key);
        }
       return  unOriginalMap.get(key);

    }

}

4.创建上下文请求

public class PayRefundContext {

    @Resource
    private RefundStrategy payRefundStrategy;

    public PayRefundContext(RefundStrategy payRefundStrategy) {
        this.payRefundStrategy = payRefundStrategy;
    }

    public PayResult executePayRefundStrategy(String tradeNo, String refundNo,String orderNo, String thirdpartPayNo, String totalAmount, String refundAmount, String refundReason) {
       return payRefundStrategy.doPayRefund(tradeNo, refundNo, orderNo, thirdpartPayNo, totalAmount, refundAmount, refundReason);
    }

}

5.测试类

  @Test
    public void executePayRefundStrategy() {

        PayRefundContext payRefundContext = new PayRefundContext(RefundStrategyFactory.getPayRefundStrategy(18, true));
        PayResult payResult = payRefundContext.executePayRefundStrategy(
                "1111","1111","1111", "1111","111","111","1111"
        );
    }
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 第一部分 HTML&CSS整理答案 1. 什么是HTML5? 答:HTML5是最新的HTML标准。 注意:讲述HT...
    kismetajun阅读 27,820评论 1 45
  • 工厂模式类似于现实生活中的工厂可以产生大量相似的商品,去做同样的事情,实现同样的效果;这时候需要使用工厂模式。简单...
    舟渔行舟阅读 7,910评论 2 17
  • 西安大学客阅读 44评论 0 0
  • 对于微商行业来讲,首先要明确本文所讲的是狭义的微商,即通过微信等移动端社交平台直接销售产品的电商行为;并且直销、保...
    毛正奇阅读 110评论 0 0
  • 天空是蓝色的 红 云朵是白色的 红 高山是青色的 红 流水是透明的 红 红酒是心醉的 红 爱情是玫瑰的 红 幼年是...
    公子九月回阅读 177评论 0 0