news 2026/4/16 10:36:41

状态模式(State):订单状态机(待支付 → 已支付 → 已发货)在 Laravel 中如何实现?是否使用状态模式?

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
状态模式(State):订单状态机(待支付 → 已支付 → 已发货)在 Laravel 中如何实现?是否使用状态模式?

Laravel 本身并未内置状态机组件,但“订单状态流转”这类场景正是状态模式(State Pattern)的经典用武之地。虽然 Laravel 核心未强制使用状态模式,但在复杂业务系统中,通过状态模式实现订单状态机是推荐的最佳实践,它能有效避免“巨型 if-else”或“状态硬编码”,使状态转换逻辑清晰、可扩展、可测试。


一、状态模式的核心思想(GoF 定义)

允许一个对象在其内部状态改变时改变它的行为。对象看起来似乎修改了它的类

  • Context(上下文):拥有状态的对象(如Order);
  • State(状态接口):定义状态行为(如handlePayment());
  • ConcreteState(具体状态):实现具体行为(如PendingState,PaidState);
  • 关键将状态相关的行为封装在状态类中,而非 Context 中

在订单系统中:

  • Context=Order模型;
  • State=OrderState接口;
  • ConcreteState=PendingState,PaidState,ShippedState

二、Laravel 中实现订单状态机的两种方式

方式 1:简单状态字段(反模式)
// app/Models/Order.phpclassOrderextendsModel{publicfunctionmarkAsPaid(){if($this->status!=='pending'){thrownewLogicException('Order is not pending');}$this->status='paid';$this->save();}publicfunctionship(){if($this->status!=='paid'){thrownewLogicException('Order is not paid');}$this...status='shipped';$this->save();}}
问题:
  • 状态逻辑散落在模型中
  • 新增状态需修改模型(违反开闭原则);
  • 状态转换规则隐式(需阅读代码才能知道paid → shipped是否合法);
  • 难以测试(需构造特定状态)。

这是“状态字段 + 行为方法”的混合体,不是状态模式


方式 2:状态模式实现(推荐)
步骤 1:定义状态接口
// app/Orders/OrderState.phpinterfaceOrderState{publicfunctionmarkAsPaid(Order$order):void;publicfunctionship(Order$order):void;publicfunctioncancel(Order$order):void;}
步骤 2:实现具体状态
// app/Orders/States/PendingState.phpclassPendingStateimplementsOrderState{publicfunctionmarkAsPaid(Order$order):void{$order->status='paid';$order->state=newPaidState();// ← 关键:状态对象变更$order->save();}publicfunctionship(Order$order):void{thrownewLogicException('Cannot ship pending order');}publicfunctioncancel(Order$order):void{$order->status='cancelled';$order->state=newCancelledState();$order->save();}}// app/Orders/States/PaidState.phpclassPaidStateimplementsOrderState{publicfunctionmarkAsPaid(Order$order):void{thrownewLogicException('Order is already paid');}publicfunctionship(Order$order):void{$order->status='shipped';$order->state=newShippedState();$order->save();}publicfunctioncancel(Order$order):void{// 已支付订单取消需退款RefundService::process($order);$order->status='cancelled';$order->state=newCancelledState();$order->save();}}
步骤 3:Order 模型持有状态对象
// app/Models/Order.phpclassOrderextendsModel{protected$casts=['state'=>StateCaster::class,// ← 自定义 caster 序列化状态];publicfunction__construct(array$attributes=[]){parent::__construct($attributes);$this->state=$this->state??newPendingState();}publicfunctionmarkAsPaid():void{$this->state->markAsPaid($this);}publicfunctionship():void{$this->state->ship($this);}}
步骤 4:自定义 Caster(序列化状态对象)
// app/Orders/StateCaster.phpclassStateCasterimplementsCastsAttributes{publicfunctionget($model,string$key,$value,array$attributes){if(!$value)returnnewPendingState();$class='App\\Orders\\States\\'.ucfirst($attributes['status']).'State';returnnew$class();}publicfunctionset($model,string$key,$value,array$attributes){return$value::class;// 存储类名}}

状态行为由状态类封装,Order 模型仅委托调用


三、状态模式 vs 简单状态字段

特性简单状态字段状态模式
状态行为散落在模型中封装在状态类中
新增状态修改模型(违反 OCP)新增状态类(符合 OCP)
状态规则隐式(需读代码)显式(状态类即文档)
可测试性需构造状态直接测试状态类
复杂逻辑模型臃肿逻辑分散到状态类

状态模式让“状态转换”成为一等公民


四、Laravel 生态中的状态机包

虽然可手动实现,但 Laravel 社区有成熟的状态机包:

1.spatie/laravel-model-states(推荐)
  • 专为 Eloquent 设计;
  • 支持状态转换、守卫、事件;
  • 无需手动管理状态对象。
示例:
// app/Models/Order.phpuseSpatie\ModelStates\HasStates;classOrderextendsModel{useHasStates;protectedfunctionregisterStates():void{$this->addState('status',OrderStatus::class)->allowTransition(Pending::class,Paid::class)->allowTransition(Paid::class,Shipped::class)->allowTransition([Paid::class,Shipped::class],Cancelled::class);}}// app/Models/States/OrderStatus.phpabstractclassOrderStatusextendsState{abstractpublicfunctionlabel():string;}classPendingextendsOrderStatus{publicfunctionlabel():string{return'Pending';}publicfunctionpay():void{$this->model->transitionTo(Paid::class);}}

这是状态模式的现代化、Laravel 化实现

2.winzou/state-machine
  • 更通用的状态机库;
  • 需手动集成到模型。

五、与你工程理念的深度对齐

你的原则在状态模式中的体现
关注点分离状态行为与模型数据分离
可扩展性新增状态无需修改现有代码
可测试性状态类可独立单元测试
避免硬编码状态转换规则显式声明
SOLID 遵循符合开闭原则(OCP)、单一职责(SRP)

六、何时使用状态模式?

场景推荐方式
简单状态(< 3 个,无复杂逻辑)简单状态字段 + 守卫方法
复杂状态机(> 3 个状态,有转换规则、副作用)状态模式spatie/laravel-model-states
需要审计日志、事件、守卫使用状态机包

状态模式不是银弹,但在复杂业务中是必要解耦手段


七、完整最佳实践示例(使用 Spatie 包)

1. 安装
composerrequire spatie/laravel-model-states
2. 定义状态
// app/Models/States/OrderStatus.phpabstractclassOrderStatusextendsState{abstractpublicfunctioncanBePaid():bool;abstractpublicfunctionpay(Order$order):void;}classPendingextendsOrderStatus{publicfunctioncanBePaid():bool{returntrue;}publicfunctionpay(Order$order):void{$order->transitionTo(Paid::class);Mail::to($order->user)->send(newOrderPaidMail($order));}}classPaidextendsOrderStatus{publicfunctioncanBePaid():bool{returnfalse;}publicfunctionpay(Order$order):void{thrownewLogicException('Already paid');}}
3. 模型集成
classOrderextendsModel{useHasStates;protectedfunctionregisterStates():void{$this->addState('status',OrderStatus::class)->default(Pending::class)->allowTransition(Pending::class,Paid::class);}}
4. 使用
$order=Order::find(1);if($order->status->canBePaid()){$order->status->pay($order);}

状态行为、转换规则、副作用全部封装,模型保持纯净


结语

虽然 Laravel 核心未内置状态模式,但在订单、工单、审批流等复杂状态场景中,状态模式是解决“状态爆炸”和“逻辑混乱”的利器。它通过:

状态接口 + 具体状态类 + 上下文委托

实现了:

  • 状态行为的封装与隔离
  • 状态转换规则的显式声明
  • 业务逻辑的高内聚、低耦合

正如你所坚持的:好的架构不是预测所有变化,而是让变化发生时,修改最小化
状态模式正是这一理念的典范——当你新增一个“已退货”状态,只需写一个类,订单模型一行代码不动

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/16 11:00:19

鸿蒙(HarmonyOS)第一个程序开发全指南:环境搭建+入门例程

鸿蒙&#xff08;HarmonyOS&#xff09;第一个程序开发全指南&#xff1a;环境搭建入门例程 一、前言 本文基于 HarmonyOS 4.1&#xff08;API 10&#xff09;&#xff08;官方推荐稳定版&#xff09;&#xff0c;采用 Stage开发模型 和 ArkTS语言&#xff08;鸿蒙原生开发主力…

作者头像 李华
网站建设 2026/4/15 7:18:20

ImageJ终极指南:5步快速掌握免费科学图像分析工具

ImageJ终极指南&#xff1a;5步快速掌握免费科学图像分析工具 【免费下载链接】ImageJ Public domain software for processing and analyzing scientific images 项目地址: https://gitcode.com/gh_mirrors/im/ImageJ 想要在科研工作中轻松处理显微镜图像吗&#xff1f…

作者头像 李华
网站建设 2026/4/16 10:45:16

python人工智能AI问答时代个人计算机的安全防护科普系统_cl98b307

目录已开发项目效果实现截图开发技术路线相关技术介绍核心代码参考示例结论源码lw获取/同行可拿货,招校园代理 &#xff1a;文章底部获取博主联系方式&#xff01;已开发项目效果实现截图 同行可拿货,招校园代理 python人工智能AI问答时代个人计算机的安全防护科普系统_cl98…

作者头像 李华
网站建设 2026/4/16 10:41:01

29、数据持久化与.NET泛型编程

数据持久化与.NET泛型编程 1. 数据序列化相关知识 数据序列化是将对象转换为可传输或存储的格式的过程,在这个过程中有许多要点需要掌握。 1.1 序列化接口的实现 当实现 System.Runtime.Serialization.ISerializable 接口时, BinaryFormatter 在序列化或反序列化对象时…

作者头像 李华
网站建设 2026/4/15 0:20:33

WordPress Markdown编辑器WP-Editor.md:让写作回归纯粹与高效

WordPress Markdown编辑器WP-Editor.md&#xff1a;让写作回归纯粹与高效 【免费下载链接】WP-Editor.md 或许这是一个WordPress中最好&#xff0c;最完美的Markdown编辑器 项目地址: https://gitcode.com/gh_mirrors/wp/WP-Editor.md 还在为WordPress默认编辑器的复杂格…

作者头像 李华
网站建设 2026/4/14 10:14:48

终极指南:在Jupyter中轻松运行C代码的10个技巧

终极指南&#xff1a;在Jupyter中轻松运行C代码的10个技巧 【免费下载链接】jupyter-c-kernel Minimal Jupyter C kernel 项目地址: https://gitcode.com/gh_mirrors/ju/jupyter-c-kernel 你是否想过&#xff0c;将C语言这种系统级编程语言的强大功能&#xff0c;与Jupy…

作者头像 李华