模版设计模式示例

第一步,定义基础模版方法。

package com.ucf.trade.process;

import java.util.HashMap;
import java.util.Map;

import com.ucf.platform.framework.core.log.UcfLogger;
import com.ucf.platform.framework.core.log.UcfLoggerFactory;
import com.ucf.trade.business.IBusiness;
import com.ucf.trade.request.impl.TradeRequest;
import com.ucf.trade.response.impl.TradeResponse;
import com.ucf.trade.util.enums.TradeEnum;
import com.ucf.trade.util.exception.TradeBusinessException;


public abstract class BaseProcess {
	private final static UcfLogger logger = UcfLoggerFactory.getLogger(BaseProcess.class);
	/*
	 * 定义流程名称,记录日志时使用
	 */
	protected String processName;
	/*
	 * 控制流程流转
	 * 如果设置为下一个action的序列,则跳过中间action
	 * 如果action中不设置,默认为0顺序执行
	 * 如果设置为-1则直接退出
	 */
	protected int gotoStep = 0;
	
	/*
	 * 定义全部action的执行顺序
	 */
	public Map<Integer, IBusiness> flow = new HashMap<Integer, IBusiness>();
	
	/*
	 * 接口流程执行入口
	 */
	public void exec(TradeRequest request,TradeResponse response) throws Exception {
		//组装处理流程
		assembleAction();
		/*
		 * 顺序执行action
		 * 通过捕获TradeBusinessException结束流程的执行
		 * 
		 */
		try {
 			for(int i = 1;i < flow.size() + 1;i ++) {
				if(gotoStep == -1) {
					break;
				}
				if(gotoStep > 0 ) {
					if(gotoStep < i) {
						i = gotoStep;
					}
					if(gotoStep > flow.size()) {
						gotoStep = flow.size();
					}
					if(gotoStep == i) {
						this.setGotoStep(0);
						flow.get(i).exec(request, response, this);
						this.flow(i,response);
					} 
					continue;
				}
				this.setGotoStep(0);
				flow.get(i).exec(request, response, this);
				this.flow(i,response);
			}
		} catch (TradeBusinessException e) {
			logger.error(e.getMessage(), e);
			assembleException(e, response);
		}
	}
	/*
	 * 由子类实现,进行流程组装
	 */
	public abstract void assembleAction();
	
	/*
	 * 由子类实现,进行流程组装
	 */
	public abstract void flow(int i,TradeResponse tradeResponse);
	
	/*
	 * 将异常信息包装到返回对象中
	 */
	public void assembleException(TradeBusinessException e,TradeResponse response) {
		response.setResult(TradeEnum.FAIL.getCode());
		response.setMessage(TradeEnum.FAIL.getName());
		response.setExceptionCode(e.getErrorCode());
		response.setExceptionMessage(e.getMessage());
	}
	/*
	 * getter and setter 
	 */
	public int getGotoStep() {
		return gotoStep;
	}
	public void setGotoStep(int gotoStep) {
		this.gotoStep = gotoStep;
	}
	public String getProcessName() {
		return processName;
	}
	public void setProcessName(String processName) {
		this.processName = processName;
	}
}

package com.ucf.trade.business;

import com.ucf.trade.process.BaseProcess;
import com.ucf.trade.request.impl.TradeRequest;
import com.ucf.trade.response.impl.TradeResponse;

public interface IBusiness {
	public void exec(TradeRequest tradeRequest,TradeResponse tradeResponse, BaseProcess process) throws Exception;
}


第二步:只体的实现模版子类:

package com.ucf.trade.process;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.ucf.trade.business.recharge.RechargeCreateOrder;
import com.ucf.trade.business.recharge.RechargeCreateSuborder;
import com.ucf.trade.business.recharge.RechargeQueryChannel;
import com.ucf.trade.business.recharge.RechargeSendMq;
import com.ucf.trade.business.recharge.RechargeToPayment;
import com.ucf.trade.business.recharge.RechargeVerification;
import com.ucf.trade.business.recharge.RechargeWhiteList;
import com.ucf.trade.response.impl.TradeResponse;
@Component
@Scope("prototype")
public class RechargeProcess extends BaseProcess {
	public RechargeProcess(){
		this.setProcessName("一步后台充值");
	}
    @Resource
	private RechargeVerification rechargeVerification;
    @Resource
    private RechargeWhiteList  rechargeWhiteList;
    @Resource
    private RechargeCreateOrder rechargeCreateOrder;
    @Resource
    private RechargeQueryChannel rechargeQueryChannel;
    @Resource
    private RechargeCreateSuborder rechargeCreateSuborder;
    @Resource
    private RechargeToPayment rechargeToPayment;
    @Resource
    private RechargeSendMq rechargeSendMq;
	@Override
	public void assembleAction() {
		this.flow.put(1, rechargeVerification);
		this.flow.put(2, rechargeWhiteList);
		this.flow.put(3, rechargeCreateOrder);
		this.flow.put(4, rechargeQueryChannel);
		this.flow.put(5, rechargeCreateSuborder);
		this.flow.put(6, rechargeToPayment);
		this.flow.put(7, rechargeSendMq);

	}

	@Override
	public void flow(int i, TradeResponse tradeResponse) {
		if(i == 3) {
			if(null!=tradeResponse.getResult()) {
				this.setGotoStep(-1);
			}
		}
	}

}

只体的某一个类,可以自己去实现。

第三步:定义service 接口服

package com.ucf.trade.dubbo.service;

import org.springframework.stereotype.Service;

import com.ucf.trade.dubbo.bo.RechargeParamBo;
import com.ucf.trade.dubbo.bo.RechargeReturnBo;
 
@Service
public interface RechargeService {
	 
	public RechargeReturnBo recharge(RechargeParamBo rechargeParamBo);

}

具体的入参和出参可以自己编写。

第四步:定义service的实现类

package com.ucf.trade.dubbo.service.impl;

import java.lang.reflect.InvocationTargetException;

import javax.annotation.Resource;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.ucf.platform.framework.core.log.UcfLogger;
import com.ucf.platform.framework.core.log.UcfLoggerFactory;
import com.ucf.trade.dubbo.bo.RechargeParamBo;
import com.ucf.trade.dubbo.bo.RechargeReturnBo;
import com.ucf.trade.dubbo.service.RechargeService;
import com.ucf.trade.process.RechargeProcess;
import com.ucf.trade.request.impl.TradeRequest;
import com.ucf.trade.response.impl.TradeResponse;
import com.ucf.trade.util.enums.TradeEnum;
import com.ucf.trade.util.exception.ErrorCode;
@Service
public class RechargeServiceImpl implements RechargeService {
	private final static UcfLogger loggerBiz = UcfLoggerFactory.getLogger("COMMON.BIZ");
	private final static UcfLogger logger = UcfLoggerFactory.getLogger(RechargeServiceImpl.class);
    @Resource
	private RechargeProcess rechargeProcess;
	@Override
	public RechargeReturnBo recharge(RechargeParamBo rechargeParamBo) {
	 
		TradeRequest request = new TradeRequest();
		TradeResponse response = new TradeResponse();
		RechargeReturnBo rechargeReturnBo = new RechargeReturnBo();
		
		//设置默认参数
		rechargeParamBo.setBizProduct(TradeEnum.BIZ_PRODUCT_MERCHANT.getCode());
		rechargeParamBo.setBusinessType(TradeEnum.TRANS_TYPE_RECHARGE.getCode());
		rechargeParamBo.setPayProduct(TradeEnum.PAY_PRODUCT_BACKRECHARGE.getCode());
		
		//入参BO对象封装到request中
		request.setRechargeParamBo(rechargeParamBo);
		
		try {
			rechargeProcess.exec(request, response);
		} catch(Exception e) {
			/*
			 * 此处捕获为不可预知异常,测试充分上线后不应该出现此类异常
			 * 
			 */
			logger.error(e.getMessage(), e);
			rechargeReturnBo.setResult(TradeEnum.FAIL.getCode());
			rechargeReturnBo.setMessage(TradeEnum.FAIL.getName());
			rechargeReturnBo.setExceptionCode(ErrorCode.OUT_ERROR_99999.getCode());
			rechargeReturnBo.setExceptionMessage(ErrorCode.OUT_ERROR_99999.getMsg());
			return rechargeReturnBo;
		}
		//response对象数据封装到出参BO对象中
		try {
			BeanUtils.copyProperties(rechargeReturnBo, rechargeParamBo);
			BeanUtils.copyProperties(rechargeReturnBo, response);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		loggerBiz.info("【一步后台充值返回结果】{}",JSON.toJSONString(rechargeReturnBo));
		return rechargeReturnBo;
	}

}
异常类定义:

package com.ucf.trade.util.exception;


public class TradeBusinessException extends Exception {

	private static final long serialVersionUID = 7927874095862109971L;
	protected String errorCode;
	public TradeBusinessException(ErrorCode errorCode) {
		this(errorCode.getCode(),errorCode.getMsg());
	}
	public String getErrorCode() {
		return this.errorCode;
	}

	public TradeBusinessException() {
		super();
	}

	public TradeBusinessException(String errorCode) {
		super();
		this.errorCode = errorCode;
	}

	public TradeBusinessException(String errorCode, String errorMessage) {
		super(errorMessage);

		this.errorCode = errorCode;
	}

	public TradeBusinessException(String errorCode, String errorMessage, Throwable cause) {
		super(errorMessage, cause);
		this.errorCode = errorCode;
	}

	public TradeBusinessException(Throwable cause) {
		super(cause);
	}

}


以上代码只供自己参考。

你可能感兴趣的:(模版设计模式示例)