责任链模式
责任链模式是一种行为设计模式, 允许你将请求沿着处理者链进行发送。 收到请求后, 每个处理者均可对请求进行处理, 或将其传递给链上的下个处理者。
策略模式
策略模式是一种行为设计模式, 它能让你定义一系列算法, 并将每种算法分别放入独立的类中, 以使算法的对象能够相互替换。
/*** @Description 核心处理逻辑抽象类* @Author * @Version 1.0**/
public abstract class AbstractHandler {/*** 定义下一个执行器*/private AbstractHandler next;private HdOrderStrategy hdOrderStrategy;/*** 赋值下一个执行器* @param next*/public void setNext(AbstractHandler next) {this.next = next;}/*** 获取下一个执行器* @return*/public AbstractHandler getNext() {return next;}/*** 执行逻辑* @param* @return*/public abstract boolean excute(CustomerOrderDTO receiveHdDTO) throws Exception;/*** 执行下一个处理器逻辑* @param* @return*/protected boolean excuteNextHandler(CustomerOrderDTO receiveHdDTO) throws Exception {if(this.getNext() != null){return this.getNext().excute(receiveHdDTO);}return true;}/*** 定义执行器的名称* @param* @return*/public String getHandlerName(){return this.getClass().getName();}/*** 赋值下一个执行器* @param hdOrderStrategy*/public AbstractHandler setHdOrderStrategy(HdOrderStrategy hdOrderStrategy) {this.hdOrderStrategy = hdOrderStrategy;return this;}/*** 获取下一个执行器* @return*/public HdOrderStrategy getHdOrderStrategy() {return hdOrderStrategy;}}
/*** @Description 处理公共父类* @Author yhb* @Date 2024/02/02* @Version**/
@Slf4j
@Service
public class OrderBaseStategy implements HdOrderStrategy {@Autowiredprotected HdOrderHandler hdOrderHandler;@Autowiredprotected HdOrderAccountHandler hdOrderAccountHandler;/*** 公共处理链路* @param receiveHdOrderDTO*/@Overridepublic CustomerOrderDTO receiveHdOrders(CustomerOrderDTO receiveHdOrderDTO) {log.info("生成订单入参:{}", JSON.toJSONString(receiveHdOrderDTO));try {//处理订单hdOrderHandler.setHdOrderStrategy(this).setNext(hdOrderAccountHandler);//处理账户hdOrderAccountHandler.setHdOrderStrategy(this).setNext(specialHandle());//执行hdOrderHandler.excute(receiveHdOrderDTO);}catch (Exception e) {log.error("接收订单处理异常:{}", e.getMessage());receiveHdOrderDTO.setResultCode("ERROR");receiveHdOrderDTO.setResultMsg(e.getMessage());}return receiveHdOrderDTO;}/*** 空处理链路* @return*/public AbstractHandler specialHandle(){return new AbstractHandler() {@Overridepublic boolean excute(CustomerOrderDTO receiveHdDTO) throws Exception {return true;}@Overridepublic String getHandlerName() {return "空处理链路";}};};
}
```bash
/*** @Description 订单基础信息* @Version**/
@Component("hdOrderHandler")
@Slf4j
public class HdOrderHandler extends AbstractHandler {/*** 执行逻辑** @param customerOrderDTO* @return*/@Overridepublic boolean excute(CustomerOrderDTO customerOrderDTO) throws Exception {log.info("生成订单order入参:{}", JSON.toJSONString(customerOrderDTO));if (OrderStrategyEnum.CODE_ERROR.getStrategyBean().equals(customerOrderDTO.getResultCode())){return this.excuteNextHandler(customerOrderDTO);}try{//订单税率customerOrderDTO.setTaxRate("13%");//出账维度customerOrderDTO.setBillingDimension("地市级");}catch(Exception e){log.info("生成订单异常:", e);customerOrderDTO.setResultCode(OrderStrategyEnum.CODE_ERROR.getStrategyBean());customerOrderDTO.setResultMsg(OrderStrategyEnum.MSG_ORDDER.getStrategyBean());}return this.excuteNextHandler(customerOrderDTO);}}
/*** @Description 生成订单合同信息* @Version**/
@Component("hdOrderAccountHandler")
@Slf4j
public class HdOrderAccountHandler extends AbstractHandler {@Resourceprivate OrderInfoMapper orderInfoMapper;@Resourceprivate OrderFlowProcessServiceImpl orderFlowProcessServiceImpl;/*** 执行逻辑** @param customerOrderDTO* @return*/@Overridepublic boolean excute(CustomerOrderDTO customerOrderDTO) throws Exception {log.info("生成订单account入参:{}", JSON.toJSONString(customerOrderDTO));if (OrderStrategyEnum.CODE_ERROR.getStrategyBean().equals(customerOrderDTO.getResultCode())){return this.excuteNextHandler(customerOrderDTO);}try{//开始创建或者查询客户账户信息TODOcustomerOrderDTO.setCustOrderAccount(nyCustAccountUser.getCustAccount());customerOrderDTO.setCustOrderUser(nyCustAccountUser.getCustUser());}}catch(Exception e){log.info("生成订单异常:", e);customerOrderDTO.setResultCode(OrderStrategyEnum.CODE_ERROR.getStrategyBean());customerOrderDTO.setResultMsg(OrderStrategyEnum.MSG_ACCOUNT.getStrategyBean());}return this.excuteNextHandler(customerOrderDTO);}
}
/*** @Description 处理订单自定义策略接口* @Version**/
public interface HdOrderStrategy {/*** 处理核心方法* @param* @return*/CustomerOrderDTO receiveHdOrders(CustomerOrderDTO receiveHdOrderDTO) ;
}