優(yōu)雅解決大量if判斷
一:前言
大家都在項(xiàng)目上遇到大量的if判斷影響代碼的可讀性和復(fù)雜度睬罗,對(duì)于后期不好維護(hù)等問(wèn)題。下面如何解決大量if條件讓代碼看起來(lái)很優(yōu)雅
二:項(xiàng)目需求
我在項(xiàng)目里有個(gè)集成支付接口旭斥,比如支付寶容达、微信、銀聯(lián)等支付接口垂券。在前端入?yún)⒌闹Ц额愋突ㄑ危蠖烁鶕?jù)支付類型調(diào)用封裝好的支付接口,這樣一來(lái)代碼就會(huì)存在難以維護(hù)的if校驗(yàn)菇爪。
三:引入策略設(shè)計(jì)模式解決if判斷
項(xiàng)目中的實(shí)例
1.創(chuàng)建一個(gè)interface接口
public interface RefundStrategy {
/**
*
* @param tradeNo 交易流水號(hào)
* @param refundNo 退款單號(hào)
* @param totalAmount 訂單總金額
* @param refundAmount 退款金額
* @param refundReason 退款原因
* @return 退款結(jié)果集 {@link PayResult}
*/
PayResult doPayRefund(String tradeNo, String refundNo, String orderNo,String thirdpartPayNo, String totalAmount, String refundAmount, String refundReason);
}
2.在Ali實(shí)現(xiàn)類實(shí)現(xiàn)該接口
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);
//獲取退款交易流水號(hào)
@SuppressWarnings("unchecked")
Map<String, String> map = commonUtil.readValue(payResult.getParams(), Map.class);
payResult.setTradeNo(map.get("trade_no"));
return payResult;
}
3.創(chuàng)建策略工廠
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.創(chuàng)建上下文請(qǐng)求
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.測(cè)試類
@Test
public void executePayRefundStrategy() {
PayRefundContext payRefundContext = new PayRefundContext(RefundStrategyFactory.getPayRefundStrategy(18, true));
PayResult payResult = payRefundContext.executePayRefundStrategy(
"1111","1111","1111", "1111","111","111","1111"
);
}