策略工厂模式的运用实例(策略模式+工厂模式)

配置文件 :


    <!-- 代扣策略工厂 -->
    <bean id="withholdStrategyFactory" class="*******WithholdStrategyFactory" >
        <property name="withholdStrategyMap" >
            <map>
                  <entry key="default" value-ref="jydWithholdHandleService"></entry>
                <entry key="1" value-ref="jydWithholdHandleService"></entry>
                <entry key="2" value-ref="lsdWithholdHandleService"></entry>
                <entry key="3" value-ref="yxdWithholdHandleService"></entry>
                <entry key="4" value-ref="htPsWithholdHandleService"></entry>                
            </map>
        </property>
    </bean>
代码:

1.策略工厂:

public class WithholdStrategyFactory {

    private Map<String, WithholdHandleService> withholdStrategyMap;

    public Map<String, WithholdHandleService> getWithholdStrategyMap() {
        return withholdStrategyMap;
    }

    public void setWithholdStrategyMap(Map<String, WithholdHandleService> withholdStrategyMap) {
        this.withholdStrategyMap = withholdStrategyMap;
    }

    public WithholdHandleService getInstance(String productProvider) {
        if (null == productProvider) {
            throw new RuntimeException("资金方不能为空");
        }
        if (null == withholdStrategyMap.get(productProvider)) {
            throw new RuntimeException("代扣未找到对应的实例bean:" + productProvider);
        }
        return withholdStrategyMap.get(productProvider);
    }
}

2.接口 

public interface WithholdHandleService {

    /**
     *代扣接口
     */
    WithholdAcceptResult withHoldFusionpay(TradeOperFlowBean tradeFlow, IouBean iou, String signNo, ProviderPaymentParams params);

}

3.具体运用:

        withholdStrategyFactory.getInstance(params.getProductProvider()).withHoldFusionpay(tradeFlow, iou, signNo,params);
4.具体实现代码的实现 :

public class JydWithholdHandleServiceImpl implements WithholdHandleService {

public WithholdAcceptResult withHoldFusionpay(TradeOperFlowBean tradeFlow, IouBean iou, String signNo,
            ProviderPaymentParams params) {
 
                iou.getIouId(), signNo, JsonUtil.toJSONString(params));
        WithholdAcceptResult r = null;
        try {
            WithholdRequestParam payRequestParam = createWithholdRequestParam(tradeFlow, iou, signNo, params);
            if (StringUtils.isBlank(payRequestParam.getSign())) {
               
                r = WithholdAcceptResult.fail(ErrCode.PARAM_ERR.name(), "sign异常,发送代扣请求终止");
            }
            logger.info("http代扣请求入参:{}", JsonUtil.toJSONString(payRequestParam));
            PayBizResponse res = paymentHttpService.withHoldFusionpay(payRequestParam);
            logger.info("http代扣请求结果:{}", JsonUtil.toJSONString(res));
            tradeFlow.setReturnMsg(res.getRetMsg());
            if (PayResult.SUCCESS.getCode().equals(res.getRetCode())) {
                // 受理成功
                updateTradeFlowToProcessing(tradeFlow);
                r = WithholdAcceptResult.ok("受理成功");
            } else {
                // 受理失败
                updateTradeFlowToFailure(tradeFlow);
                r = WithholdAcceptResult.fail(ErrCode.WITHHOLD_NOT_ACCEPTED.name(), "http代扣请求受理失败");
            }

        } catch (Exception e) {
   
            String msg = e.getMessage();
            if (msg != null && msg.length() > 256) {
               
                msg = msg.substring(0, 256);
            }
            tradeFlow.setRemarks(msg);
            updateTradeFlowToRetry(tradeFlow);
            r = WithholdAcceptResult.ok("处理中");
        } finally {
           
            iouMapper.increaseVersion(iou.getIouId());
        }
        return r;
    }

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值