优雅解决大量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"
);
}