工厂+策略+责任链+模板方法设计模式重构审单流程实战

工厂+策略+责任链+模板方法设计模式实战

  • 1.策略+模板方法模式搭建审单流程主体
  • 2.责任链模式完成审单校验
  • 3.工厂模式完成策略选择
  • 4.业务类

需求:
系统之前只支持正常订单(在本系统中处理的订单),后面又有新需求,增加了分销订单(交给第三方处理的),然后之前用if-else实现了,但逻辑结构已经混乱了,扩展起来很复杂,所以使用设计模式进行重构这块代码

注意:正常订单和分销订单只有审单这块逻辑不同

1.策略+模板方法模式搭建审单流程主体

/**
 * @description:订单处理策略
 */
public interface OrderProcessStrategy {

    /**
     * 审核订单处理
     * @param orderDO
     */
    void checkOrderProcess(OrderDO orderDO);
}

/**
 * @description:订单公共处理逻辑
 */
public abstract class BaseOrderProcessStrategy implements OrderProcessStrategy{
    @Autowired
    LogMapper logMapper;
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    StoreMapper storeMapper;
    @Autowired
    WarehouseMapper warehouseMapper;
    @Autowired
    private StoreWarehouseMapper storeWarehouseMapper;

    @Override
    public void checkOrderProcess(OrderDO orderDO) {
        //前置处理
        preProcess(orderDO);

        //具体处理
        doProcess(orderDO);

        //后置处理
        doLog(orderDO);
    }

    public abstract void doProcess(OrderDO orderDO);

    public void preProcess(OrderDO orderDO){
        //发货仓库匹配
        Long warehouseId = storeWarehouseMapper.selectEnableWarehouse(orderDO.getStoreId());
        orderDO.setWarehouseId(warehouseId);

        //快递设置
        StoreDO storeDO = storeMapper.selectById(orderDO.getStoreId());
        orderDO.setExpressId(storeDO.getExpressId());
    }

    public void doLog(OrderDO orderDO){
        LogDO logDO = new LogDO();
        logDO.setAppName("order-service");
        logDO.setStatus((byte) 0);
        logDO.setEditTableId(orderDO.getId());
        logDO.setEditTableName(BASIC_ORDER);
        logDO.setCreateUserId(1L);
        logDO.setCreateTime(CommonUtil.getCurrentDate());
        logDO.setCreateUserName(SystemConstant.SYSTEM_NAME);
        logDO.setRemark("订单审核成功");
        logMapper.insert(logDO);
    }
}

/**
 * @description:正常订单
 */
@Component("normalOrderProcessStrategy")
public class NormalOrderProcessStrategy extends BaseOrderProcessStrategy {
    @Autowired
    private AddressCheckHandler addressCheckHandler;
    @Autowired
    private ExpressCheckHandler expressCheckHandler;
    @Autowired
    private ProductStockCheckHandler productStockCheckHandler;

    @Override
    public void doProcess(OrderDO orderDO) {
        //责任链模式校验
        CheckHandler checkHandler = addressCheckHandler
                .appendHandler(expressCheckHandler.appendHandler(productStockCheckHandler));
        checkHandler.doCheck(orderDO);
    }
}

/**
 * @description:分销订单
 */
@Component("distributionOrderProcessStrategy")
public class DistributionOrderProcessStrategy extends BaseOrderProcessStrategy {
    @Autowired
    private AddressCheckHandler addressCheckHandler;
    @Autowired
    private ProductStockCheckHandler productStockCheckHandler;

    @Override
    public void doProcess(OrderDO orderDO) {
        //责任链模式校验
        CheckHandler checkHandler = addressCheckHandler.appendHandler(productStockCheckHandler);
        checkHandler.doCheck(orderDO);
    }
}

2.责任链模式完成审单校验

/**
 * @description:审单校验
 */
@Component
public abstract class CheckHandler {

    private CheckHandler handler;

    public CheckHandler next() {
        return this.handler;
    }

    public CheckHandler appendHandler(CheckHandler handler) {
        this.handler = handler;
        return this;
    }

    /**
     * 审单
     *
     * @param orderDO 订单主信息
     * @return
     */
    public abstract void doCheck(OrderDO orderDO);

}

/**
 * @Description: 收货地址校验
 */
@Slf4j
@Component("addressCheckCommand")
public class AddressCheckHandler extends CheckHandler {

    @Override
    public void doCheck(OrderDO orderDO) {

        //校验收货地址信息

        CheckHandler next = super.next();
        //当前为最后一个规则,结束
        if (null == next) {
            return;
        }

        next.doCheck(orderDO);
    }
}

/**
 * @Description: 发货快递校验
 */
@Slf4j
@Component("matchExpressCheckCommand")
public class ExpressCheckHandler extends CheckHandler {

    @Override
    public void doCheck(OrderDO orderDO) {

        //正常单需校验发货快递信息
        //分销单是交由第三方处理不用校验发货快递

        CheckHandler next = super.next();
        //当前为最后一个规则,结束
        if (null == next) {
            return;
        }

        next.doCheck(orderDO);
    }
}

/**
 * @Description: 商品库存校验
 */
@Slf4j
@Component("productStockCheckCommand")
public class ProductStockCheckHandler extends CheckHandler {

    @Override
    public void doCheck(OrderDO orderDO) {

        if (orderDO.getOrderType()=="normal"){
            //正常订单校验商品库存

        }else {
            //分销订单校验分销库存

        }

        CheckHandler next = super.next();
        //当前为最后一个规则,结束
        if (null == next) {
            return;
        }

        next.doCheck(orderDO);
    }
}

3.工厂模式完成策略选择

/**
 * @description:工厂类
 */
@Component
public class OrderProcessStrategyFactory {

    @Autowired
    private ApplicationContext applicationContext;

    public OrderProcessStrategy getStrategy(String type) {
        return applicationContext.getBean(type + "OrderProcessStrategy", OrderProcessStrategy.class);
    }

}

4.业务类

@Service
public class OrderProcessService {
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    RedissonClient redissonClient;
    @Autowired
    private OrderProcessStrategyFactory strategyFactory;

    public void checkOrderStatus(OrderDO orderDO) {
        RLock lock = redissonClient.getLock(String.format(CacheKeyConstant.REDISSON_CLIENT_PUBLIC_CACHE, "order",
                "checkOrderStatus"));
        try {
            lock.lock();
            List<OrderDO> orderList = orderMapper.selectList(
                    new LambdaQueryWrapper<OrderDO>()
                            .eq(OrderDO::getOrderState, 0)
                            .eq(OrderDO::getDataStatus, OrderDataStatusConstant.INIT)
                            .eq(OrderDO::getDataCheckStatus, -1)
            );

            //审单
            orderList.stream().forEach(order -> {
                //选择处理策略
                OrderProcessStrategy strategy = strategyFactory.getStrategy(order.getOrderType());
                //执行处理流程
                strategy.checkOrderProcess(order);
            });

        } finally {
            if (lock.isLocked() && lock.isHeldByCurrentThread()) {
                lock.unlock();
            }
        }
    }
}

你可能感兴趣的:(设计模式,设计模式,重构,工厂模式,责任链模式,策略模式,模板方法模式)