代码优雅之道——用统一风格的模式演绎秒杀系统程序设计
前言
看过老版《三国演绎》的都知道,这部历史题材的鸿篇巨制获得巨大成功,除了演员的精湛演技,导演的匠心制作,忠于原著的文白式的历史还原,还有就是统一的演绎风格————统一的道具,统一服装,统一的场景布局,统一摄制......这些统一的演绎风格,正是电视剧的优雅之处!
代码的优雅之道也是如此,只要有统一的编程风格和模式,程序也是一道靓丽的风景!
优雅代码模板设计
VO、DTO、DO使用
VO:前端传输使用主要应用于business服务中的Controller类
DTO:业务聚合服务调用原子服务时使用,多为原子服务Controller类的请求参数和返回结果
DO:数据映射类Mapper类使用
服务调用约定
服务方返回:Response<T>
示例:
@RequestMapping(value = MemberConstants.MEMBER_AUTH_LONGINFO_REQ_PATH, method = RequestMethod.POST)
public Response<AuthUserRspDTO> getAuthUser(@RequestParam(value = "phone", required = true)String phone){
String serviceName = "AuthQryController.getAuthUser";
return webActionTemplate.execute(new AbstractWebAction<AuthUserRspDTO, String>(serviceName,phone) {
@Override
public void validateParams(String param) throws BizException {
AuthQryValidator.doValidQryParam(param);
}
@Override
public AuthUserRspDTO doAction(String param) throws BizException {
AuthUserBO authUserBO = authUserQryService.getAuthUser(param);
AuthUserRspDTO rspDTO = AuthUserConvertor.bo2DTO(authUserBO);
return rspDTO;
}
});
}
调用方处理:
public AuthUserRspDTO getAuthUser(String phone) {
Response<AuthUserRspDTO> rspRst = authRemote.getAuthUser(phone);
if(rspRst == null){
throw new RuntimeException("用户服务获取认证用户信息远程调用返回异常");
}
if(!rspRst.isSuccess()){
throw new RuntimeException("用户服务获取认证用户信息远程调用失败");
}
return rspRst.getData();
}
常见的业务处理步骤可参考如下,实际处理根据业务需要酌情修改
public Integer createAuth(AuthUserBO userBo) {
Map<String, Object> bizData = new HashMap<>();
//业务验证
doVaildAuth(userBo, bizData);
//防重验证
doCheckRepeat(userBo);
//组装处理数据
doHandleData(userBo, bizData);
//三方服务调用
doInvokeRemote(userBo, bizData);
//数据库处理
doCommitDBAuth(bizData);
//事件发送
doPublishEvent(bizData);
return SUCCESS_CODE;
}
2.3.4 通用的业务处理模板可参考如下,实际处理根据业务需要酌情修改
public FacadeResponse doTrigger(FacadeRequest req) {
FacadeResponse response = new FacadeResponse();
Map<String,Object> bizMap = new HashMap<>();
//参数验证
doCheck(req);
//防重验证
boolean reapet = doRepeat(req);
if(reapet){
response.setSuccess(true);
logger.info("[AbstractActionFacade.doReapet] request:{},response:{}"
,JSON.toJSONString(req),JSON.toJSONString(response));
return response;
}
//业务验证
boolean checkBizRst = doCheckBiz(req,bizMap);
if(!checkBizRst){
response.setSuccess(false);
logger.info("[AbstractActionFacade.doCheckBiz] check fail request:{},response:{}"
, JSON.toJSONString(req),JSON.toJSONString(response));
return response;
}
try {
//数据组装
doAssemble(req, bizMap);
//三方调用
doInvokeRemote(req, bizMap);
//数据库提交
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
doCommitDatabase(req, bizMap);
}
});
//发布事件
doPublishEvent(req, bizMap);
//通知
doNotify(req, bizMap);
response.setSuccess(true);
}catch (BizException ex) {
doPublishErrorEvent(req, bizMap);
response.setSuccess(false);
logger.error("[AbstractActionFacade.BizException] message:{}", ExceptionTracker.trace(ex));
throw ex;
}catch (TransactionSystemException ex){
logger.error("[AbstractActionFacade.TransactionSystemException] transaction error message:{}",ExceptionTracker.trace(ex));
throw new BizException(SysCodeEnum.DB_TRANSACTION_ERROR);
}catch (RuntimeException ex){
doPublishErrorEvent(req, bizMap);
response.setSuccess(false);
logger.error("[AbstractActionFacade.RuntimeException] message:{}",ExceptionTracker.trace(ex));
throw ex;
}catch (Exception ex){
doPublishErrorEvent(req, bizMap);
response.setSuccess(false);
logger.error("[AbstractActionFacade.Exception] message:{}",ExceptionTracker.trace(ex),ex);
throw ex;
}
return response;
}
protected abstract boolean doRepeat(FacadeRequest req) throws BizException;
protected abstract boolean doCheckBiz(FacadeRequest req,Map<String,Object> bizMap)throws BizException;
protected abstract void doAssemble(FacadeRequest req,Map<String,Object> bizMap)throws BizException;
protected abstract void doInvokeRemote(FacadeRequest req,Map<String,Object> bizMap)throws BizException;
protected abstract void doCommitDatabase(FacadeRequest req,Map<String,Object> bizMap)throws BizException;
protected abstract void doPublishEvent(FacadeRequest req,Map<String,Object> bizMap)throws BizException;
protected abstract void doNotify(FacadeRequest req,Map<String,Object> bizMap)throws BizException;
protected abstract void doPublishErrorEvent(FacadeRequest req,Map<String,Object> bizMap);