search
HomeBackend DevelopmentPHP Tutorial关于电商网站购物车设计模式的问题?

最近在重构公司的购物车,发现购物车里面商品有很多种活动。订单级、商品级活动,而且订单级活动里面包含单品级活动(比如:单品类满赠活动,里面某个商品还有单品直降活动)。
现在想用一种扩展性维护性都比较好的设计模式重新梳理一下代码,个位能给一些思路么?

回复内容:

最近在重构公司的购物车,发现购物车里面商品有很多种活动。订单级、商品级活动,而且订单级活动里面包含单品级活动(比如:单品类满赠活动,里面某个商品还有单品直降活动)。
现在想用一种扩展性维护性都比较好的设计模式重新梳理一下代码,个位能给一些思路么?

正好最近在做一个项目 楼上说各种设计模式 呵呵 想太多了
我们的设计 复杂到什么程度
(1)常规打折
(2)批发价
(3)会员价
(4)单品优惠券减免
(5)全单优惠券减免
(6)满减满赠满折
(7)限时促销
(8)积分
....
最后的逻辑是触发任何一个修改就重新计算整个购物车的价格 而且你设计模式 这里面互斥多的很呢。。装饰器写着简单 你能互斥么 比如我会员价批发价打折3选1你怎么装饰器
ecshop的购物车写的够复杂了 我们这个比他还复杂 等等 你以为这就完了
如果用户没选活动怎么办
如果商品下架了怎么办
如果商品在车里突然价格变了或者活动结束了怎么办
tmd简直是个无底洞

所以 打死不在数据库里存价格 计算量大是小 算错钱了才是麻烦事
而且 就算你在数据库里存价格 后面一致性的问题写死你 还不如直接实时算
结账的时候肯定要所有数据校验一次 那还不如当初就不算

购物车我认为是没法简化的 设计模式不显示 上面说用插件的 用什么插件 所有的活动无外乎列出来的几种 你需要的是让使用者用你给的活动组合出需要的活动 尽可能简化设计
活动更多的不是叠加 而是互斥 所以装饰器不现实
如果你做N选1自动算最优惠更恶心 解决方案就是像京东 把活动选择权给用户

真正写代码就是面向过程的 把你的整个优惠流程梳理出来 然后按照这个逻辑写出来 而不是考虑面向对象的方法

使用插件模式,每次需要办一个活动,就当做一个插件。然后可以方便的加载和卸载,这样活动不会影响核心的代码。
同求大神解读

每办一次活动, 加入购物车中的商品, 计算出最后的价格, 肯定是不一样的。 所以关键是要先算出活动之后的商品的价格,然后插入数据表中。 可以考虑代理模式,和装饰者模式同时使用。
代理模式: 主要用在购物车业务逻辑层, 在数据插入购物车之前算出商品价格。
装饰者模式: 应该是促销常用的一种计算价格的解决方案。 可以实现动态为对象添加功能, 比如:VIP 9折, 全场所有商品9折两个活动一起用,用这个模式最好了。。。

filterchain类似这种的

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

How to make PHP applications fasterHow to make PHP applications fasterMay 12, 2025 am 12:12 AM

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

PHP Performance Optimization Checklist: Improve Speed NowPHP Performance Optimization Checklist: Improve Speed NowMay 12, 2025 am 12:07 AM

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

PHP Dependency Injection: Improve Code TestabilityPHP Dependency Injection: Improve Code TestabilityMay 12, 2025 am 12:03 AM

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

PHP Performance Optimization: Database Query OptimizationPHP Performance Optimization: Database Query OptimizationMay 12, 2025 am 12:02 AM

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool