当前位置: 首页 > news >正文

设计模式在实际业务中应用 - 模版方法

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 {@Overrideprotected void shelvesThirdPlatformStoreGoods(ThirdPlatformGoods localStoreGoods) {// 饿了么平台商品上架}@Overrideprotected void withdrawThirdPlatformStoreGoods(ThirdPlatformGoods localStoreGoods) {// 饿了么平台商品下架}@Overrideprotected StoreGoodsInfoResult getThirdPlatformStoreGoods(StoreGoodsRefreshParam storeGoodsRefreshParam,ThirdPlatformGoods localStoreGoods) {// 获取饿了么平台商品信息return null;}@Overrideprotected void increaseThirdPlatformStoreGoodsStock(StoreGoodsIncreaseStockParam storeGoodsIncreaseStockParam,ThirdPlatformGoods localStoreGoods) {// 增加饿了么平台商品库存}@Overrideprotected void decreaseThirdPlatformStoreGoodsStock(StoreGoodsDecreaseStockParam storeGoodsDecreaseStockParam, ThirdPlatformGoods localStoreGoods) {// 减少饿了么平台商品库存}@Overrideprotected 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 {@Resourceprivate ThirdPlatformGoodsTemplateManage thirdPlatformGoodsTemplateManage;@Overridepublic Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {return bean;}@Overridepublic 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. 为什么选择模版方法

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

  • 从功能角度来说对三方平台商品管理无非就是那么几个商品新增、商品编辑、商品库存调整、商品上下架等;
  • 从商品领域建模对三方平台商品领域行为抽象可分为:创建、修改、增加库存、减少库存、覆盖库存、上架、下架等;
  • 从代码复用的角度来说不同平台商品的操作不同的只有最终调用三方平台的差异上,其他代码都可复用,假设第一次开发只先适配美团,那么在美团适配后复用的代码理论上不需要做任何修改;
  • 从代码可扩展角度来说,如果增加其他平台,仅需要继承模版抽象类,实现抽象方法即可;
  • 从关注点分离角度来说后续适配其他平台仅仅需要关注如何操作三方平台即可,这对新人或者不了解的人来继续迭代有很大好处,关注点集中在如何适配三方平台上;

相关文章:

设计模式在实际业务中应用 - 模版方法

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

BGP综合实验

任务如下&#xff1a; 1.AS1存在两个环回&#xff0c;一个地址为192.168.1.0/24该地址不能在任何协议中宣告 AS3存在两个环回&#xff0c;一个地址为192.168.2.0/24该地址不能在任何协议中宣告&#xff0c;最终要求这两个环回可以互相通讯 2.整个AS2的IP地址为172.16.0.0/16&…...

Global Surface Summary of the Day 全球逐日气象站点数据 GSOD数据集

数据名称 Global Surface Summary of the Day 数据内容 数据包含以下气象要素的日值观测数据&#xff1a; 气压&#xff1a;平均气压、海平面气压&#xff1b;气温&#xff1a;平均气温、日最高气温、日最低气温&#xff1b;湿度&#xff1a;露点温度&#xff08;需自行换算…...

Harmony OS4开发入门

代码地址&#xff1a; https://gitee.com/BruceLeeAdmin/harmonyos/tree/master 项目目录介绍 ArkTS介绍 简单案例&#xff1a; State times: number 0/*数据类型&#xff1a;stringnumberany: 不确定类型&#xff0c;可以是任意类型*/State msg: string "hello"…...

.net core 事务

在 .NET Core 中&#xff0c;可以使用 Entity Framework Core 来实现事务处理。下面是一个简单的示例&#xff0c;展示了如何在 .NET Core 中使用 Entity Framework Core 来创建和执行事务&#xff1a; using System; using Microsoft.EntityFrameworkCore; using System.Tran…...

【Python】python天气数据抓取与数据分析(源码+论文)【独一无二】

&#x1f449;博__主&#x1f448;&#xff1a;米码收割机 &#x1f449;技__能&#x1f448;&#xff1a;C/Python语言 &#x1f449;公众号&#x1f448;&#xff1a;测试开发自动化【获取源码商业合作】 &#x1f449;荣__誉&#x1f448;&#xff1a;阿里云博客专家博主、5…...

MPPT工作流程及算法和硬件的选择

MPPT算法选择 目前&#xff0c;MPPT算法有开路电压比率(离线)、短路电流比率(离线)、观察调节(在线)、极限追踪控制法(在线)。 在光伏控制系统中&#xff0c;因为日照、温度等条件的变化&#xff0c;光伏电池的输出功率也是在不断变化的&#xff0c;为保证使得光伏电池的输出功…...

C#,《小白学程序》第十九课:随机数(Random)第六,随机生成任意长度的大数(BigInteger)

1 文本格式 using System; using System.Linq; using System.Text; using System.Collections.Generic; /// <summary> /// 大数的&#xff08;加减乘除&#xff09;四则运算、阶乘运算 /// 乘法计算包括小学生算法、Karatsuba和Toom-Cook3算法 /// 除法运算为 Truffer…...

每日一练【移动零】

一、题目描述 283. 移动零 - 力扣&#xff08;LeetCode&#xff09; 给定一个数组 nums&#xff0c;编写一个函数将所有 0 移动到数组的末尾&#xff0c;同时保持非零元素的相对顺序。 请注意 &#xff0c;必须在不复制数组的情况下原地对数组进行操作。 二、题目解析 可以…...

QT修改windowTitle的名字以及图片

1.修改名字:点击ui的QMainWindow,然后找到windowTitle的选项修改即可 2.修改windowTitle的图片,依旧是找到windowIcon,选择资源,这个资源可以是你放到qrc里面的图片也可以是外置的图片 3.然后运行就可以看到效果了...

C语言-指针讲解(3)

文章目录 1.字符指针变量1.1 字符指针变量类型是什么1.2字符指针变量的两种使用方法&#xff1a;1.3字符指针笔试题讲解1.3.1 代码解剖 2.数组指针变量2.1 什么是数组指针2.2 数组指针变量是什么&#xff1f;2.2.3 数组指针变量的举例 2.3数组指针和指针数组的区别是什么&#…...

慢 SQL 分析及优化

目录 分析慢 SQL SQL 优化 单表优化 多表优化 慢 SQL&#xff1a;指 MySQL 中执行比较慢的 SQL排查慢 SQL 最常用的方法&#xff1a;通过慢查询日志来查找慢 SQL MySQL 的慢查询日志是 MySQL 提供的一种日志记录&#xff0c;它用来记录在 MySQL 中响应时间超过阈值的语句&…...

PTA:计算m到n之间所有素数的和

题目 计算m到n之间所有素数的和&#xff0c;其中 2 < m <n <100 输入格式: 请在这里写输入格式。例如&#xff1a;输入两个正整数 输出格式: 请在这里描述输出格式。例如&#xff1a;输出两个正整数之间的素数和。 样例 输入样例: 在这里给出一组输入。例如&…...

Golang实现YOLO:高性能目标检测算法

引言 目标检测是计算机视觉领域的重要任务&#xff0c;它不仅可以识别图像中的物体&#xff0c;还可以标记出物体的位置和边界框。YOLO&#xff08;You Only Look Once&#xff09;是一种先进的目标检测算法&#xff0c;以其高精度和实时性而闻名。本文将介绍如何使用Golang实…...

文档 + 模型

文档 模型 0: 基于MATLAB的soc锂电池系统设计 1: 电力系统继电保护仿真设计-毕业论文 2: 继电保护仿真-三段式电流保护的方案设计及分析-相间短路 3: 直流电机双闭环控制系统 转速电流双闭环调速 4: matlab电力系统继电保护仿真 三段电流保护仿真-双侧电源系统 5: OFDM-MIMO课…...

计算机毕业设计php+bootstrap小区物业管理系统

意义&#xff1a;随着我国经济的发展和人们生活水平的提高&#xff0c;住宅小区已经成为人们居住的主流&#xff0c;人们生活质量提高的同时&#xff0c;对小区物业管理的要求也越来越高&#xff0c;诸如对小区的维修维护&#xff0c;甚至对各项投诉都要求小区管理者做得好&…...

Osg线程模型(选择不当,会引发崩溃)

来自《最长的一帧》...

2161根据数字划分数组

给你一个下标从 0 开始的整数数组 nums 和一个整数 pivot 。请你将 nums 重新排列&#xff0c;使得以下条件均成立&#xff1a; 所有小于 pivot 的元素都出现在所有大于 pivot 的元素 之前 。所有等于 pivot 的元素都出现在小于和大于 pivot 的元素 中间 。小于 pivot 的元素之…...

Oracle Linux 9.3 发布

导读Oracle Linux 9 系列发布了第 3 个版本更新&#xff0c;支持 64 位 Intel 和 AMD (x86_64) 以及 64 位 Arm (aarch64) 平台。与所有的 Oracle Linux 版本一样&#xff0c;此版本与相应 RHEL 版本 100% 应用二进制兼容。 对于 x86_64 和 aarch64 架构&#xff0c;Oracle Li…...

XML Schema中的simpleContent 元素

XML Schema中的simpleContent 元素出现在complexType 内部&#xff0c;是对complexType 的一种扩展、或者限制。 simpleContent 元素在complexType元素内部最多只能出现1次。 simpleContent元素下面必须包含1个restriction或者extension元素。 例如&#xff0c;下面的Schema片…...

web vue 项目 Docker化部署

Web 项目 Docker 化部署详细教程 目录 Web 项目 Docker 化部署概述Dockerfile 详解 构建阶段生产阶段 构建和运行 Docker 镜像 1. Web 项目 Docker 化部署概述 Docker 化部署的主要步骤分为以下几个阶段&#xff1a; 构建阶段&#xff08;Build Stage&#xff09;&#xff1a…...

深入剖析AI大模型:大模型时代的 Prompt 工程全解析

今天聊的内容&#xff0c;我认为是AI开发里面非常重要的内容。它在AI开发里无处不在&#xff0c;当你对 AI 助手说 "用李白的风格写一首关于人工智能的诗"&#xff0c;或者让翻译模型 "将这段合同翻译成商务日语" 时&#xff0c;输入的这句话就是 Prompt。…...

(十)学生端搭建

本次旨在将之前的已完成的部分功能进行拼装到学生端&#xff0c;同时完善学生端的构建。本次工作主要包括&#xff1a; 1.学生端整体界面布局 2.模拟考场与部分个人画像流程的串联 3.整体学生端逻辑 一、学生端 在主界面可以选择自己的用户角色 选择学生则进入学生登录界面…...

以下是对华为 HarmonyOS NETX 5属性动画(ArkTS)文档的结构化整理,通过层级标题、表格和代码块提升可读性:

一、属性动画概述NETX 作用&#xff1a;实现组件通用属性的渐变过渡效果&#xff0c;提升用户体验。支持属性&#xff1a;width、height、backgroundColor、opacity、scale、rotate、translate等。注意事项&#xff1a; 布局类属性&#xff08;如宽高&#xff09;变化时&#…...

云启出海,智联未来|阿里云网络「企业出海」系列客户沙龙上海站圆满落地

借阿里云中企出海大会的东风&#xff0c;以**「云启出海&#xff0c;智联未来&#xff5c;打造安全可靠的出海云网络引擎」为主题的阿里云企业出海客户沙龙云网络&安全专场于5.28日下午在上海顺利举办&#xff0c;现场吸引了来自携程、小红书、米哈游、哔哩哔哩、波克城市、…...

【网络安全产品大调研系列】2. 体验漏洞扫描

前言 2023 年漏洞扫描服务市场规模预计为 3.06&#xff08;十亿美元&#xff09;。漏洞扫描服务市场行业预计将从 2024 年的 3.48&#xff08;十亿美元&#xff09;增长到 2032 年的 9.54&#xff08;十亿美元&#xff09;。预测期内漏洞扫描服务市场 CAGR&#xff08;增长率&…...

连锁超市冷库节能解决方案:如何实现超市降本增效

在连锁超市冷库运营中&#xff0c;高能耗、设备损耗快、人工管理低效等问题长期困扰企业。御控冷库节能解决方案通过智能控制化霜、按需化霜、实时监控、故障诊断、自动预警、远程控制开关六大核心技术&#xff0c;实现年省电费15%-60%&#xff0c;且不改动原有装备、安装快捷、…...

Frozen-Flask :将 Flask 应用“冻结”为静态文件

Frozen-Flask 是一个用于将 Flask 应用“冻结”为静态文件的 Python 扩展。它的核心用途是&#xff1a;将一个 Flask Web 应用生成成纯静态 HTML 文件&#xff0c;从而可以部署到静态网站托管服务上&#xff0c;如 GitHub Pages、Netlify 或任何支持静态文件的网站服务器。 &am…...

ffmpeg(四):滤镜命令

FFmpeg 的滤镜命令是用于音视频处理中的强大工具&#xff0c;可以完成剪裁、缩放、加水印、调色、合成、旋转、模糊、叠加字幕等复杂的操作。其核心语法格式一般如下&#xff1a; ffmpeg -i input.mp4 -vf "滤镜参数" output.mp4或者带音频滤镜&#xff1a; ffmpeg…...

【配置 YOLOX 用于按目录分类的图片数据集】

现在的图标点选越来越多&#xff0c;如何一步解决&#xff0c;采用 YOLOX 目标检测模式则可以轻松解决 要在 YOLOX 中使用按目录分类的图片数据集&#xff08;每个目录代表一个类别&#xff0c;目录下是该类别的所有图片&#xff09;&#xff0c;你需要进行以下配置步骤&#x…...