关于电商网站购物车设计模式的问题?
最近在重构公司的购物车,发现购物车里面商品有很多种活动。订单级、商品级活动,而且订单级活动里面包含单品级活动(比如:单品类满赠活动,里面某个商品还有单品直降活动)。
现在想用一种扩展性维护性都比较好的设计模式重新梳理一下代码,个位能给一些思路么?
回复内容:
最近在重构公司的购物车,发现购物车里面商品有很多种活动。订单级、商品级活动,而且订单级活动里面包含单品级活动(比如:单品类满赠活动,里面某个商品还有单品直降活动)。
现在想用一种扩展性维护性都比较好的设计模式重新梳理一下代码,个位能给一些思路么?
正好最近在做一个项目 楼上说各种设计模式 呵呵 想太多了
我们的设计 复杂到什么程度
(1)常规打折
(2)批发价
(3)会员价
(4)单品优惠券减免
(5)全单优惠券减免
(6)满减满赠满折
(7)限时促销
(8)积分
....
最后的逻辑是触发任何一个修改就重新计算整个购物车的价格 而且你设计模式 这里面互斥多的很呢。。装饰器写着简单 你能互斥么 比如我会员价批发价打折3选1你怎么装饰器
ecshop的购物车写的够复杂了 我们这个比他还复杂 等等 你以为这就完了
如果用户没选活动怎么办
如果商品下架了怎么办
如果商品在车里突然价格变了或者活动结束了怎么办
tmd简直是个无底洞
所以 打死不在数据库里存价格 计算量大是小 算错钱了才是麻烦事
而且 就算你在数据库里存价格 后面一致性的问题写死你 还不如直接实时算
结账的时候肯定要所有数据校验一次 那还不如当初就不算
购物车我认为是没法简化的 设计模式不显示 上面说用插件的 用什么插件 所有的活动无外乎列出来的几种 你需要的是让使用者用你给的活动组合出需要的活动 尽可能简化设计
活动更多的不是叠加 而是互斥 所以装饰器不现实
如果你做N选1自动算最优惠更恶心 解决方案就是像京东 把活动选择权给用户
真正写代码就是面向过程的 把你的整个优惠流程梳理出来 然后按照这个逻辑写出来 而不是考虑面向对象的方法
使用插件模式,每次需要办一个活动,就当做一个插件。然后可以方便的加载和卸载,这样活动不会影响核心的代码。
同求大神解读
每办一次活动, 加入购物车中的商品, 计算出最后的价格, 肯定是不一样的。 所以关键是要先算出活动之后的商品的价格,然后插入数据表中。 可以考虑代理模式,和装饰者模式同时使用。
代理模式: 主要用在购物车业务逻辑层, 在数据插入购物车之前算出商品价格。
装饰者模式: 应该是促销常用的一种计算价格的解决方案。 可以实现动态为对象添加功能, 比如:VIP 9折, 全场所有商品9折两个活动一起用,用这个模式最好了。。。
filterchain类似这种的

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4
