本文介绍: 作者在工作中主要主导 A 业务线的系统建设,A 业务线主要是零售场景酒水的售卖与即时配送服务。为了方便运营在自研系统中对多平台商品进行管理开发的三方平台商品管理功能,本次介绍模版方法模式则是在该功能开发过程中的落地实践。

1. 业务背景

作者在工作中主要主导 A 业务线的系统建设,A 业务线主要是零售场景酒水的售卖与即时配送服务。为了方便运营在自研系统中对多平台商品进行管理而开发的三方平台商品管理功能,本次介绍模版方法模式则是在该功能开发过程中的落地实践。

2. 技术方案选型

该业务场景可选设计三种

从系统建设角度来说最好的选择是第一种,但是对于一个业务方想要更快的提升运营效率来说,更好选择是第二种和第三种我们从自身业务情况、性能、后续向第一种方案演进的角度选择了第二种方案。

3. 模版方法应用

以下代码仅为演示对模版方法应用展示代码设计思路,有部分方法并未实现,但通过注释说明方法内做了什么操作

3.1. 模版抽象

@Component
public abstract class ThirdPlatformStoreGoodsOperations {

    /**
     * 商品上架
     * @param storeGoodsShelvesParam 商品上架参数
     * @return 新商品记录ID
     */
    public Long shelves(StoreGoodsShelvesParam storeGoodsShelvesParam) {

        // 获取门店本地商品
        ThirdPlatformGoods localStoreGoods = this.getLocalStoreGoods(storeGoodsShelvesParam.getId());
        if (localStoreGoods == null) {
            throw new HistoricalDataException("商品数据更新,请刷新当前页面");
        }

        // 上架三方平台门店商品
        this.shelvesThirdPlatformStoreGoods(localStoreGoods);

        // 刷新本地商品
        StoreGoodsRefreshParam storeGoodsRefreshParam = new StoreGoodsRefreshParam();
        BeanUtils.copyProperties(storeGoodsShelvesParam, storeGoodsRefreshParam);
        return this.refreshOne(storeGoodsRefreshParam);
    }

    /**
     * 商品下架
     * @param storeGoodsWithdrawParam 商品下架参数
     * @return 新商品记录ID
     */
    public Long withdraw(StoreGoodsWithdrawParam storeGoodsWithdrawParam) {

        // 获取门店本地商品
        ThirdPlatformGoods localStoreGoods = this.getLocalStoreGoods(storeGoodsWithdrawParam.getId());
        if (localStoreGoods == null) {
            throw new HistoricalDataException("商品数据更新,请刷新当前页面");
        }

        // 下架三方平台门店商品
        this.withdrawThirdPlatformStoreGoods(localStoreGoods);

        // 刷新本地商品
        StoreGoodsRefreshParam storeGoodsRefreshParam = new StoreGoodsRefreshParam();
        BeanUtils.copyProperties(storeGoodsWithdrawParam, storeGoodsRefreshParam);
        return this.refreshOne(storeGoodsRefreshParam);
    }

    /**
     * 增加商品库存
     * @param storeGoodsIncreaseStockParam 增加商品库存参数
     * @return 新商品记录ID
     */
    public Long increaseStock(StoreGoodsIncreaseStockParam storeGoodsIncreaseStockParam) {

        // 获取门店本地商品
        ThirdPlatformGoods localStoreGoods = this.getLocalStoreGoods(storeGoodsIncreaseStockParam.getId());
        if (localStoreGoods == null) {
            throw new HistoricalDataException("商品数据更新,请刷新当前页面");
        }

        // 增加三方平台店铺商品库存
        this.increaseThirdPlatformStoreGoodsStock(storeGoodsIncreaseStockParam, localStoreGoods);

        // 刷新本地商品
        StoreGoodsRefreshParam storeGoodsRefreshParam = new StoreGoodsRefreshParam();
        BeanUtils.copyProperties(storeGoodsIncreaseStockParam, storeGoodsRefreshParam);
        return this.refreshOne(storeGoodsRefreshParam);
    }

    /**
     * 增加商品库存
     * @param storeGoodsDecreaseStockParam 减少商品库存参数
     * @return 新商品记录ID
     */
    public Long decreaseStock(StoreGoodsDecreaseStockParam storeGoodsDecreaseStockParam) {

        // 获取门店本地商品
        ThirdPlatformGoods localStoreGoods = this.getLocalStoreGoods(storeGoodsDecreaseStockParam.getId());
        if (localStoreGoods == null) {
            throw new HistoricalDataException("商品数据已更新,请刷新当前页面");
        }

        // 减少三方平台店铺商品库存
        this.decreaseThirdPlatformStoreGoodsStock(storeGoodsDecreaseStockParam, localStoreGoods);

        // 刷新本地商品
        StoreGoodsRefreshParam storeGoodsRefreshParam = new StoreGoodsRefreshParam();
        BeanUtils.copyProperties(storeGoodsDecreaseStockParam, storeGoodsRefreshParam);
        return this.refreshOne(storeGoodsRefreshParam);
    }

    /**
     * 刷新店铺单个商品
     * @param storeGoodsRefreshParam 刷新店铺单个商品参数
     * @return 新商品记录ID
     */
    public Long refreshOne(StoreGoodsRefreshParam storeGoodsRefreshParam) {

        // 获取门店本地商品
        ThirdPlatformGoods localStoreGoods = this.getLocalStoreGoods(storeGoodsRefreshParam.getId());
        if (localStoreGoods == null) {
            throw new HistoricalDataException("商品数据已更新,请刷新当前页面");
        }

        // 获取三方门店商品
        StoreGoodsInfoResult storeGoodsInfoResult = this.getThirdPlatformStoreGoods(storeGoodsRefreshParam, localStoreGoods);

        // 刷新本地商品
        return this.refreshLocalStoreGoods(storeGoodsRefreshParam, storeGoodsInfoResult);
    }

    /**
     * 刷新店铺商品
     * @param storeGoodsRefreshParam 刷新店铺单个商品参数
     * @param storeGoodsInfoResult 三方门店商品
     */
    private Long refreshLocalStoreGoods(StoreGoodsRefreshParam storeGoodsRefreshParam,
                                        StoreGoodsInfoResult storeGoodsInfoResult) {
        // 更新本地商品
    }

    /**
     * 获取本地店铺商品信息
     * @param localStoreGoodsId 本地商品ID
     * @return 本地店铺商品信息
     */
    private ThirdPlatformGoods getLocalStoreGoods(Long localStoreGoodsId) {
        // 获取本地商品
    }

    /**
     * 上架三方店铺商品
     * @param localStoreGoods 本地店铺商品信息
     */
    protected abstract void shelvesThirdPlatformStoreGoods(ThirdPlatformGoods localStoreGoods);

    /**
     * 下架三方店铺商品
     * @param localStoreGoods 本地店铺商品信息
     */
    protected abstract void withdrawThirdPlatformStoreGoods(ThirdPlatformGoods localStoreGoods);

    /**
     * 获取三方平台店铺商品
     * @param storeGoodsRefreshParam 刷新店铺单个商品参数
     * @param localStoreGoods 本地商品
     * @return 三方门店商品
     */
    protected abstract StoreGoodsInfoResult getThirdPlatformStoreGoods(StoreGoodsRefreshParam storeGoodsRefreshParam,
                                                                       ThirdPlatformGoods localStoreGoods);

    /**
     * 增加三方平台店铺商品库存
     * @param storeGoodsIncreaseStockParam 增加三方平台店铺商品库存参数
     * @param localStoreGoods 本地店铺商品信息
     */
    protected abstract void increaseThirdPlatformStoreGoodsStock(StoreGoodsIncreaseStockParam storeGoodsIncreaseStockParam,
                                                                 ThirdPlatformGoods localStoreGoods);

    /**
     * 减少三方平台店铺商品库存
     * @param storeGoodsDecreaseStockParam 减少三方平台店铺商品库存参数
     * @param localStoreGoods 本地店铺商品信息
     */
    protected abstract void decreaseThirdPlatformStoreGoodsStock(StoreGoodsDecreaseStockParam storeGoodsDecreaseStockParam,
                                                                 ThirdPlatformGoods localStoreGoods);

    /**
     * 获取对应平台
     * @return 平台枚举
     */
    protected abstract ThirdPlatformEnums getPlatform();
}

3.2. 模版实现类

public class ELEStoreGoodsTemplate extends ThirdPlatformStoreGoodsOperations {

    @Override
    protected void shelvesThirdPlatformStoreGoods(ThirdPlatformGoods localStoreGoods) {
        // 饿了么平台商品上架
    }

    @Override
    protected void withdrawThirdPlatformStoreGoods(ThirdPlatformGoods localStoreGoods) {
        // 饿了么平台商品下架
    }

    @Override
    protected StoreGoodsInfoResult getThirdPlatformStoreGoods(StoreGoodsRefreshParam storeGoodsRefreshParam,
                                                              ThirdPlatformGoods localStoreGoods) {
        // 获取饿了么平台商品信息
        return null;
    }

    @Override
    protected void increaseThirdPlatformStoreGoodsStock(StoreGoodsIncreaseStockParam storeGoodsIncreaseStockParam,
                                                        ThirdPlatformGoods localStoreGoods) {
        // 增加饿了么平台商品库存
    }

    @Override
    protected void decreaseThirdPlatformStoreGoodsStock(StoreGoodsDecreaseStockParam storeGoodsDecreaseStockParam, ThirdPlatformGoods localStoreGoods) {

        // 减少饿了么平台商品库存
    }

    @Override
    protected ThirdPlatformEnums getPlatform() {
        return ThirdPlatformEnums.ELEMO;
    }
}

4. 与 Spring 结合管理模版实现类

4.1. 基于 Spring IoC 容器依赖查找管理模版

@Component
public class ThirdPlatformGoodsTemplateManage {

    public Map<ThirdPlatformEnums, ThirdPlatformStoreGoodsOperations> templateMap = new HashMap<>(5);

    public ThirdPlatformGoodsTemplateManage(ListableBeanFactory listableBeanFactory){

        ObjectProvider<ThirdPlatformStoreGoodsOperations> beanProvider = listableBeanFactory.getBeanProvider(ThirdPlatformStoreGoodsOperations.class);
        for (ThirdPlatformStoreGoodsOperations template : beanProvider) {
            templateMap.put(template.getPlatform(), template);
        }
    }

    public ThirdPlatformStoreGoodsOperations getTemplate(ThirdPlatformEnums thirdPlatformEnum) {

        ThirdPlatformStoreGoodsOperations thirdPlatformStoreGoodsOperations = templateMap.get(thirdPlatformEnum);
        Assert.notNull(thirdPlatformStoreGoodsOperations, String.format("%s平台模版未找到!", thirdPlatformEnum.getDesc()));
        return thirdPlatformStoreGoodsOperations;
    }

    public ThirdPlatformStoreGoodsOperations getTemplate(String platformCode) {
        ThirdPlatformEnums thirdPlatformEnum = ThirdPlatformEnums.getInstanceByCode(platformCode);
        ThirdPlatformStoreGoodsOperations thirdPlatformStoreGoodsOperations = templateMap.get(thirdPlatformEnum);
        Assert.notNull(thirdPlatformStoreGoodsOperations, String.format("%s平台模版未找到!", platformCode));
        return thirdPlatformStoreGoodsOperations;
    }
}

4.2. 基于 BeanPostProcessor + 注解方式管理模版

@Component
public class ThirdPlatformStoreGoodsOperationsInitialization implements BeanPostProcessor {

    @Resource
    private ThirdPlatformGoodsTemplateManage thirdPlatformGoodsTemplateManage;

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof ThirdPlatformStoreGoodsOperations && bean.getClass().isAnnotationPresent(ThirdPlatformStoreGoodsOperationsTemplate.class)){
            ThirdPlatformStoreGoodsOperations thirdPlatformStoreGoodsOperations = (ThirdPlatformStoreGoodsOperations) bean;
            thirdPlatformGoodsTemplateManage.templateMap.put(thirdPlatformStoreGoodsOperations.getPlatform(), thirdPlatformStoreGoodsOperations);
        }
        return bean;
    }
}

改进建议: 模版CODE可通过注解属性获取

5. 为什么选择模版方法

看完代码实现再回过头来,说说选择模版方法的原因:

原文地址:https://blog.csdn.net/m0_37602117/article/details/134679059

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。

如若转载,请注明出处:http://www.7code.cn/show_5887.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注