上海百度嘉定公司網(wǎng)頁設(shè)計提升seo排名
1. 業(yè)務(wù)背景
作者在工作中主要主導(dǎo) A 業(yè)務(wù)線的系統(tǒng)建設(shè),A 業(yè)務(wù)線主要是零售場景酒水的售賣與即時配送服務(wù)。為了方便運營在自研系統(tǒng)中對多平臺商品進(jìn)行管理而開發(fā)的三方平臺商品管理功能,本次介紹的模版方法模式則是在該功能開發(fā)過程中的落地實踐。
2. 技術(shù)方案選型
該業(yè)務(wù)場景可選設(shè)計為三種:
-
自研系統(tǒng)根據(jù)自身業(yè)務(wù)形態(tài)對商品領(lǐng)域進(jìn)行抽象建模,在自研系統(tǒng)里對商品領(lǐng)域的操作最終映射到三方平臺;
-
自研系統(tǒng)對三方平臺商品進(jìn)行抽象建模,保存三方平臺商品在某個時刻的快照,所有操作均是對快照的操作,進(jìn)而最終映射到三方平臺;
-
自研系統(tǒng)充當(dāng)一個適配器和轉(zhuǎn)發(fā)層,所有對商品的操作都直接映射到三方平臺;
從系統(tǒng)建設(shè)角度來說最好的選擇是第一種,但是對于一個業(yè)務(wù)方想要更快的提升運營效率來說,更好的選擇是第二種和第三種,我們從自身業(yè)務(wù)情況、性能、后續(xù)向第一種方案演進(jìn)的角度選擇了第二種方案。
3. 模版方法應(yīng)用
以下代碼僅為演示對模版方法的應(yīng)用和展示代碼設(shè)計思路,有部分方法并未實現(xiàn),但通過注釋說明方法內(nèi)做了什么操作。
3.1. 模版抽象類
@Component
public abstract class ThirdPlatformStoreGoodsOperations {/*** 商品上架* @param storeGoodsShelvesParam 商品上架參數(shù)* @return 新商品記錄ID*/public Long shelves(StoreGoodsShelvesParam storeGoodsShelvesParam) {// 獲取門店本地商品ThirdPlatformGoods localStoreGoods = this.getLocalStoreGoods(storeGoodsShelvesParam.getId());if (localStoreGoods == null) {throw new HistoricalDataException("商品數(shù)據(jù)已更新,請刷新當(dāng)前頁面");}// 上架三方平臺門店商品this.shelvesThirdPlatformStoreGoods(localStoreGoods);// 刷新本地商品StoreGoodsRefreshParam storeGoodsRefreshParam = new StoreGoodsRefreshParam();BeanUtils.copyProperties(storeGoodsShelvesParam, storeGoodsRefreshParam);return this.refreshOne(storeGoodsRefreshParam);}/*** 商品下架* @param storeGoodsWithdrawParam 商品下架參數(shù)* @return 新商品記錄ID*/public Long withdraw(StoreGoodsWithdrawParam storeGoodsWithdrawParam) {// 獲取門店本地商品ThirdPlatformGoods localStoreGoods = this.getLocalStoreGoods(storeGoodsWithdrawParam.getId());if (localStoreGoods == null) {throw new HistoricalDataException("商品數(shù)據(jù)已更新,請刷新當(dāng)前頁面");}// 下架三方平臺門店商品this.withdrawThirdPlatformStoreGoods(localStoreGoods);// 刷新本地商品StoreGoodsRefreshParam storeGoodsRefreshParam = new StoreGoodsRefreshParam();BeanUtils.copyProperties(storeGoodsWithdrawParam, storeGoodsRefreshParam);return this.refreshOne(storeGoodsRefreshParam);}/*** 增加商品庫存* @param storeGoodsIncreaseStockParam 增加商品庫存參數(shù)* @return 新商品記錄ID*/public Long increaseStock(StoreGoodsIncreaseStockParam storeGoodsIncreaseStockParam) {// 獲取門店本地商品ThirdPlatformGoods localStoreGoods = this.getLocalStoreGoods(storeGoodsIncreaseStockParam.getId());if (localStoreGoods == null) {throw new HistoricalDataException("商品數(shù)據(jù)已更新,請刷新當(dāng)前頁面");}// 增加三方平臺店鋪商品庫存this.increaseThirdPlatformStoreGoodsStock(storeGoodsIncreaseStockParam, localStoreGoods);// 刷新本地商品StoreGoodsRefreshParam storeGoodsRefreshParam = new StoreGoodsRefreshParam();BeanUtils.copyProperties(storeGoodsIncreaseStockParam, storeGoodsRefreshParam);return this.refreshOne(storeGoodsRefreshParam);}/*** 增加商品庫存* @param storeGoodsDecreaseStockParam 減少商品庫存參數(shù)* @return 新商品記錄ID*/public Long decreaseStock(StoreGoodsDecreaseStockParam storeGoodsDecreaseStockParam) {// 獲取門店本地商品ThirdPlatformGoods localStoreGoods = this.getLocalStoreGoods(storeGoodsDecreaseStockParam.getId());if (localStoreGoods == null) {throw new HistoricalDataException("商品數(shù)據(jù)已更新,請刷新當(dāng)前頁面");}// 減少三方平臺店鋪商品庫存this.decreaseThirdPlatformStoreGoodsStock(storeGoodsDecreaseStockParam, localStoreGoods);// 刷新本地商品StoreGoodsRefreshParam storeGoodsRefreshParam = new StoreGoodsRefreshParam();BeanUtils.copyProperties(storeGoodsDecreaseStockParam, storeGoodsRefreshParam);return this.refreshOne(storeGoodsRefreshParam);}/*** 刷新店鋪單個商品* @param storeGoodsRefreshParam 刷新店鋪單個商品參數(shù)* @return 新商品記錄ID*/public Long refreshOne(StoreGoodsRefreshParam storeGoodsRefreshParam) {// 獲取門店本地商品ThirdPlatformGoods localStoreGoods = this.getLocalStoreGoods(storeGoodsRefreshParam.getId());if (localStoreGoods == null) {throw new HistoricalDataException("商品數(shù)據(jù)已更新,請刷新當(dāng)前頁面");}// 獲取三方門店商品StoreGoodsInfoResult storeGoodsInfoResult = this.getThirdPlatformStoreGoods(storeGoodsRefreshParam, localStoreGoods);// 刷新本地商品return this.refreshLocalStoreGoods(storeGoodsRefreshParam, storeGoodsInfoResult);}/*** 刷新店鋪商品* @param storeGoodsRefreshParam 刷新店鋪單個商品參數(shù)* @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 刷新店鋪單個商品參數(shù)* @param localStoreGoods 本地商品* @return 三方門店商品*/protected abstract StoreGoodsInfoResult getThirdPlatformStoreGoods(StoreGoodsRefreshParam storeGoodsRefreshParam,ThirdPlatformGoods localStoreGoods);/*** 增加三方平臺店鋪商品庫存* @param storeGoodsIncreaseStockParam 增加三方平臺店鋪商品庫存參數(shù)* @param localStoreGoods 本地店鋪商品信息*/protected abstract void increaseThirdPlatformStoreGoodsStock(StoreGoodsIncreaseStockParam storeGoodsIncreaseStockParam,ThirdPlatformGoods localStoreGoods);/*** 減少三方平臺店鋪商品庫存* @param storeGoodsDecreaseStockParam 減少三方平臺店鋪商品庫存參數(shù)* @param localStoreGoods 本地店鋪商品信息*/protected abstract void decreaseThirdPlatformStoreGoodsStock(StoreGoodsDecreaseStockParam storeGoodsDecreaseStockParam,ThirdPlatformGoods localStoreGoods);/*** 獲取對應(yīng)平臺* @return 平臺枚舉*/protected abstract ThirdPlatformEnums getPlatform();
}
3.2. 模版實現(xiàn)類
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 結(jié)合管理模版實現(xiàn)類
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;}
}
改進(jìn)建議: 模版CODE可通過注解屬性獲取
5. 為什么選擇模版方法
看完代碼實現(xiàn)再回過頭來,說說選擇模版方法的原因:
- 從功能角度來說對三方平臺商品管理無非就是那么幾個商品新增、商品編輯、商品庫存調(diào)整、商品上下架等;
- 從商品領(lǐng)域建模對三方平臺商品領(lǐng)域行為抽象可分為:創(chuàng)建、修改、增加庫存、減少庫存、覆蓋庫存、上架、下架等;
- 從代碼復(fù)用的角度來說不同平臺商品的操作不同的只有最終調(diào)用三方平臺的差異上,其他代碼都可復(fù)用,假設(shè)第一次開發(fā)只先適配美團(tuán),那么在美團(tuán)適配后復(fù)用的代碼理論上不需要做任何修改;
- 從代碼可擴(kuò)展角度來說,如果增加其他平臺,僅需要繼承模版抽象類,實現(xiàn)抽象方法即可;
- 從關(guān)注點分離角度來說后續(xù)適配其他平臺僅僅需要關(guān)注如何操作三方平臺即可,這對新人或者不了解的人來繼續(xù)迭代有很大好處,關(guān)注點集中在如何適配三方平臺上;