模块

18c33a7cfff650492112f796cd978bdc

InstanceGroup

public class InstanceGroup {

    /**
     * 将某几个组,打包为一个新的组
     *
     * @param instanceGroups
     * @return
     */
    public static InstanceGroup getInstance(InstanceGroup... instanceGroups) {
        InstanceGroup result = new InstanceGroup();
        result.instanceGroups = Arrays.stream(instanceGroups).collect(Collectors.toList());
        return result;
    }

    /**
     * 将某几个process打包为一个组
     *
     * @param processClassList
     * @return
     */
    public static InstanceGroup getInstance(Class<? extends AbstractGoodsQueryProcess>... processClassList) {
        InstanceGroup result = new InstanceGroup();
        result.processClassList = Arrays.stream(processClassList).collect(Collectors.toList());
        return result;
    }

    /**
     * 通过instanceGroups&processClassList获取一个执行器组
     *
     * @param instanceGroups   最小集依赖,即后续processClass执行过程中必须的前置依赖
     * @param processClassList 本组执行的操作
     * @return
     */
    public static InstanceGroup getInstance(InstanceGroup instanceGroup, Class<? extends AbstractGoodsQueryProcess>... processClassList) {
        InstanceGroup result = new InstanceGroup();
        result.instanceGroups = new ArrayList<>();
        result.instanceGroups.add(instanceGroup);
        result.processClassList = Arrays.stream(processClassList).collect(Collectors.toList());
        return result;
    }

    /**
     * 通过instanceGroups&processClassList获取一个执行器组
     *
     * @param instanceGroups   最小集依赖,即后续processClass执行过程中必须的前置依赖
     * @param processClassList 本组执行的操作
     * @return
     */
    public static InstanceGroup getInstance(List<InstanceGroup> instanceGroups, Class<? extends AbstractGoodsQueryProcess>... processClassList) {
        InstanceGroup result = new InstanceGroup();
        result.instanceGroups = instanceGroups;
        result.processClassList = Arrays.stream(processClassList).collect(Collectors.toList());
        return result;
    }

    private List<Class<? extends AbstractGoodsQueryProcess>> processClassList;

    private List<InstanceGroup> instanceGroups;


    public boolean isClassProcess() {
        return this.processClassList != null;
    }

    public List<Class<? extends GoodsQueryProcess>> getProcessClass() {
        List<Class<? extends GoodsQueryProcess>> result = new ArrayList<>();
        if (!CollectionUtils.isEmpty(instanceGroups)) {
            for (InstanceGroup instanceGroup : instanceGroups) {
                List<Class<? extends GoodsQueryProcess>> instanceGroupProcessClass = instanceGroup.getProcessClass();
                instanceGroupProcessClass.removeAll(result);
                result.addAll(instanceGroupProcessClass);
            }
        }
        if (!CollectionUtils.isEmpty(this.processClassList)) {
            this.processClassList.removeAll(result);
            result.addAll(this.processClassList);
        }
        return result;
    }

}

InstanceGroupConstant

public interface InstanceGroupConstant {

    //==================================base===========================================
    /**
     * 商品售卖范围过滤
     * 执行的操作
     * 1. 商品ids初始化
     * 2. 商品售卖范围初始化
     * 3. appId过滤
     * 4. carType过滤
     * 5. 司机标签过滤
     */
    InstanceGroup BASE_FILTER_GOODS_SCOPE = InstanceGroup.getInstance(GoodsIdsInitProcess.class, GoodsScopeInitProcess.class, GoodsAppIdFilter.class, GoodsCarTypeFilter.class, DriverTagFilter.class);

    /**
     * 商品信息过滤
     * 执行的操作
     * 1. 商品ids初始化
     * 2. 商品信息初始化
     * 3. 商品类目过滤
     * 4. 商品状态过滤
     */
    InstanceGroup BASE_FILTER_GOODS_INFO = InstanceGroup.getInstance(GoodsIdsInitProcess.class, GoodsInfoInitProcess.class, GoodsCategoryFilter.class, GoodsOnSaleFilter.class);

    /**
     * 子商品信息初始化
     * 执行的操作
     * 1. 商品id初始化
     * 2. 子商品初始化
     */
    InstanceGroup BASE_GOODS_SKU_INIT = InstanceGroup.getInstance(GoodsIdsInitProcess.class, GoodsSkuInfoInitProcess.class);

    /**
     * 商品SKU过滤
     * 依赖商品售卖范围初始化 BASE_INIT_GOODS_SCOPE
     * 执行的操作
     * 1. 购买条件过滤
     */
    InstanceGroup BASE_FILTER_GOODS_SKU_SCENE = InstanceGroup.getInstance(BASE_GOODS_SKU_INIT, DriverSceneFilter.class);

    /**
     * 分页&整合数据
     * 依赖售卖范围过滤、商品过滤及子商品初始化
     * 执行的操作
     * 1. 分页
     * 2. 整合数据
     */
    InstanceGroup BASE_PAGE_ASSEMBLE = InstanceGroup.getInstance(Arrays.asList(BASE_FILTER_GOODS_SCOPE, BASE_FILTER_GOODS_INFO, BASE_GOODS_SKU_INIT),
            GoodsPageProcess.class,
            GoodsAutoRenewPriceInitProcess.class,
            GoodsAssembleProcess.class);


    //=======================point========================================
    /**
     * 积分前端tag过滤
     * 依赖商品信息初始化
     * 执行的操作
     * 1. 前端tag过滤
     */
    InstanceGroup POINT_FILTER_GOODS_VIEW_TYPE = InstanceGroup.getInstance(BASE_FILTER_GOODS_INFO, GoodsViewTypeFilter.class);

    /**
     * 子商品价格过滤
     * 依赖子商品初始化
     * 执行的操作
     * 1. 价格过滤
     */
    InstanceGroup POINT_FILTER_GOODS_POINT_PRICE = InstanceGroup.getInstance(BASE_GOODS_SKU_INIT, GoodsPointPriceFilter.class);


    /**
     * 积分自定义排序
     * 依赖商品信息初始化&子商品信息初始化
     * 执行的操作
     * 1. 积分自定义排序
     */
    InstanceGroup POINT_SORT_CUSTOM = InstanceGroup.getInstance(Arrays.asList(BASE_FILTER_GOODS_INFO, BASE_GOODS_SKU_INIT), PointsGoodsSortProcess.class);


    //========================group==========================================


    InstanceGroup GROUP_FILTER_GOODS_ANT_SCOPE = InstanceGroup.getInstance(Arrays.asList(BASE_FILTER_GOODS_SCOPE, BASE_FILTER_GOODS_INFO));


}

ProcessInstanceEnum

public enum ProcessInstanceEnum {

    /**
     * 通用流程
     * 存在不符合的购买条件的商品不返回
     */
    COMMON(InstanceGroup.getInstance(
            InstanceGroupConstant.GROUP_FILTER_GOODS_ANT_SCOPE,
            InstanceGroupConstant.BASE_FILTER_GOODS_SKU_SCENE,
            InstanceGroupConstant.BASE_PAGE_ASSEMBLE
    )),


    /**
     * 通用流程
     * 与COMMON的区别是不check driverSceneFilter,上游校验
     */
    COMMON_INCLUDE_BUY_LIMIT_GOODS(InstanceGroup.getInstance(
            InstanceGroupConstant.GROUP_FILTER_GOODS_ANT_SCOPE,
            InstanceGroupConstant.BASE_PAGE_ASSEMBLE
    )),

    /**
     * 积分列表流程
     */
    POINT_CUSTOM_SORT(InstanceGroup.getInstance(
            InstanceGroupConstant.GROUP_FILTER_GOODS_ANT_SCOPE,
            InstanceGroupConstant.POINT_FILTER_GOODS_VIEW_TYPE,
            InstanceGroupConstant.POINT_FILTER_GOODS_POINT_PRICE,
            InstanceGroupConstant.POINT_SORT_CUSTOM,
            InstanceGroupConstant.BASE_PAGE_ASSEMBLE
    )),

    ;

    ProcessInstanceEnum(InstanceGroup instanceGroup) {
        this.instanceGroup = instanceGroup;
    }

    private InstanceGroup instanceGroup;


    public List<Class<? extends GoodsQueryProcess>> getProcessClass() {
        return this.instanceGroup.getProcessClass();
    }
}

使用

public class GoodsQueryCompent {
    @Resource
    private List<GoodsQueryProcess> goodsQueryProcesses;

    private Map<ProcessInstanceEnum, List<GoodsQueryProcess>> instanceMap = new ConcurrentHashMap();

    @PostConstruct
    private void init() {
        Map<Class, GoodsQueryProcess> processMap = new HashMap<>();
        for (GoodsQueryProcess goodsQueryProcess : goodsQueryProcesses) {
            processMap.put(goodsQueryProcess.getClass(), goodsQueryProcess);
        }
        for (ProcessInstanceEnum value : ProcessInstanceEnum.values()) {
            List<GoodsQueryProcess> processes = new ArrayList<>();
            for (Class<? extends GoodsQueryProcess> processClass : value.getProcessClass()) {
                processes.add(processMap.get(processClass));
            }
            instanceMap.put(value, processes);
        }
    }

    public List<GoodsSkuInfoBO> queryGoodsSkus(ProcessInstanceEnum instance, GoodsQueryCondition condition) {
        GoodsQueryContext context = new GoodsQueryContext(condition);
        List<GoodsQueryProcess> list = instanceMap.get(instance);
        for (GoodsQueryProcess goodsQueryProcess : list) {
            goodsQueryProcess.process(context);
        }
        return context.getResult();
    }

}

总结

这样处理的好处是 可以任意定义流程实例及实例顺序,来满足不同场景的需求