news 2026/7/9 18:40:29

【设计优化】卫语句、策略模式、状态模式

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
【设计优化】卫语句、策略模式、状态模式

在写业务代码时,可能出现多层 if / else,通常意味着以下问题之一或并存:
1.分支条件复杂、可读性差
2.业务规则易变、修改成本高
3.单一方法承担过多职责
卫语句、策略模式、状态模式正是针对不同“分支复杂性来源”而采用的三种典型重构手段。

一.卫语句

1.适用场景
“不满足条件就立即返回 / 抛异常”

适合用于:参数校验,前置条件校验,明显的异常路径,早退出逻辑
典型特征:if 之间没有状态变化,只是为了过滤非法情况,分支不代表不同业务策略

2.原始代码

publicdecimalCalculatePrice(Orderorder){if(order!=null){if(order.Items!=null&&order.Items.Count>0){if(!order.IsCanceled){returnorder.Items.Sum(x=>x.Price);}}}return0;}

3.卫语句重构

publicdecimalCalculatePrice(Orderorder){if(order==null)return0;if(order.Items==null||order.Items.Count==0)return0;if(order.IsCanceled)return0;returnorder.Items.Sum(x=>x.Price);}

4.优缺点

  • 极低改造成本
  • 可读性显著提升
  • 非常适合方法开头
  • 不能解决真正的业务分支爆炸
  • 不适合“不同规则 / 不同行为”的分支

二、策略模式(Strategy Pattern)

  1. 适用场景
    “同一件事,不同算法 / 不同规则”

判断标准:if / else 中每个分支:都是在“做同一件事”,但实现逻辑不同,业务规则未来可能扩展
典型示例:价格计算,折扣规则,运费计算,权限校验规则

  1. 原始 if / else 示例
publicdecimalCalculateDiscount(Orderorder){if(order.CustomerType==CustomerType.Vip)returnorder.Total*0.8m;elseif(order.CustomerType==CustomerType.Normal)returnorder.Total*0.9m;elseif(order.CustomerType==CustomerType.New)returnorder.Total;elsereturnorder.Total;}
  1. 使用策略模式
//① 抽象策略接口publicinterfaceIDiscountStrategy{decimalCalculate(Orderorder);}//② 具体策略实现publicclassVipDiscountStrategy:IDiscountStrategy{publicdecimalCalculate(Orderorder)=>order.Total*0.8m;}publicclassNormalDiscountStrategy:IDiscountStrategy{publicdecimalCalculate(Orderorder)=>order.Total*0.9m;}publicclassNewCustomerDiscountStrategy:IDiscountStrategy{publicdecimalCalculate(Orderorder)=>order.Total;}//③ 策略选择(替代 if / else)publicclassDiscountStrategyFactory{privatestaticreadonlyDictionary<CustomerType,IDiscountStrategy>_strategies=new(){{CustomerType.Vip,newVipDiscountStrategy()},{CustomerType.Normal,newNormalDiscountStrategy()},{CustomerType.New,newNewCustomerDiscountStrategy()}};publicstaticIDiscountStrategyGet(CustomerTypetype)=>_strategies[type];}//④ 调用varstrategy=DiscountStrategyFactory.Get(order.CustomerType);vardiscount=strategy.Calculate(order);
  1. 优缺点
  • 可扩展性极强
  • 每个策略逻辑清晰
  • 易测试、易维护
  • 类数量增加
  • 初期显得“设计偏重”
  • 需要额外的策略选择机制

三、状态模式(State Pattern)

  1. 适用场景
    “对象行为随状态变化而变化”

判断标准:if / else 依据的是 当前状态,同一方法在不同状态下行为不同,状态之间存在流转关系
典型示例:订单状态(新建 / 已支付 / 已发货 / 已取消),工作流,审批流程,设备状态(开机 / 关机 / 待机)

  1. 原始 if / else 示例
publicvoidPay(Orderorder){if(order.Status==OrderStatus.Created){order.Status=OrderStatus.Paid;}elseif(order.Status==OrderStatus.Paid){thrownewException("订单已支付");}elseif(order.Status==OrderStatus.Canceled){thrownewException("订单已取消");}}
  1. 使用状态模式
//① 状态接口publicinterfaceIOrderState{voidPay(OrderContextcontext);}//② 状态上下文publicclassOrderContext{publicIOrderStateState{get;set;}publicvoidPay(){State.Pay(this);}}//③ 具体状态publicclassCreatedState:IOrderState{publicvoidPay(OrderContextcontext){context.State=newPaidState();}}publicclassPaidState:IOrderState{publicvoidPay(OrderContextcontext){thrownewInvalidOperationException("订单已支付");}}publicclassCanceledState:IOrderState{publicvoidPay(OrderContextcontext){thrownewInvalidOperationException("订单已取消");}}
  1. 优缺点
  • 彻底消除状态判断
  • 状态逻辑高度内聚
  • 对复杂流程极友好
  • 类数量显著增加
  • 不适合简单状态
  • 初学者理解成本高

四、三者核心对比总结

维度卫语句策略模式状态模式
解决什么问题前置校验、异常路径不同算法/规则状态驱动行为
是否消除 if
是否面向对象
类数量不变增加大量增加
是否有状态流转
扩展性
使用成本极低

完结撒花~

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

终极指南:用Cakebrew轻松管理你的macOS包管理

终极指南&#xff1a;用Cakebrew轻松管理你的macOS包管理 【免费下载链接】Cakebrew Manage your Homebrew formulas with style using Cakebrew. 项目地址: https://gitcode.com/gh_mirrors/ca/Cakebrew 还在为复杂的命令行操作头疼吗&#xff1f;想要一个简单直观的Ho…

作者头像 李华
网站建设 2026/7/10 8:08:33

字幕搜索终极解决方案:SubFinder 3分钟快速上手指南

字幕搜索终极解决方案&#xff1a;SubFinder 3分钟快速上手指南 【免费下载链接】subfinder 字幕查找器 项目地址: https://gitcode.com/gh_mirrors/subfi/subfinder 还在为找不到合适的字幕而烦恼吗&#xff1f;SubFinder作为一款强大的字幕搜索工具&#xff0c;能够帮…

作者头像 李华
网站建设 2026/7/10 8:08:32

ComfyUI_SLK_joy_caption_two终极指南:三步实现智能字幕批量生成

ComfyUI_SLK_joy_caption_two终极指南&#xff1a;三步实现智能字幕批量生成 【免费下载链接】ComfyUI_SLK_joy_caption_two ComfyUI Node 项目地址: https://gitcode.com/gh_mirrors/co/ComfyUI_SLK_joy_caption_two 在AI内容创作领域&#xff0c;如何高效处理海量图片…

作者头像 李华
网站建设 2026/7/10 7:03:41

iOSDeviceSupport:一站式iOS设备调试支持库

iOSDeviceSupport&#xff1a;一站式iOS设备调试支持库 【免费下载链接】iOSDeviceSupport All versions of iOS Device Support 项目地址: https://gitcode.com/gh_mirrors/ios/iOSDeviceSupport 还在为Xcode无法识别设备而烦恼吗&#xff1f;iOSDeviceSupport项目提供…

作者头像 李华
网站建设 2026/7/9 20:08:09

Vue Element Plus Admin终极指南:企业级后台系统快速搭建实战

Vue Element Plus Admin终极指南&#xff1a;企业级后台系统快速搭建实战 【免费下载链接】vue-element-plus-admin A backend management system based on vue3, typescript, element-plus, and vite 项目地址: https://gitcode.com/gh_mirrors/vu/vue-element-plus-admin …

作者头像 李华
网站建设 2026/7/10 9:21:19

SeedVR2-7B视频修复模型:低成本极速部署与智能增强解决方案

SeedVR2-7B视频修复模型&#xff1a;低成本极速部署与智能增强解决方案 【免费下载链接】SeedVR2-7B 项目地址: https://ai.gitcode.com/hf_mirrors/ByteDance-Seed/SeedVR2-7B 在数字内容爆炸式增长的时代&#xff0c;视频修复与增强技术正从专业领域走向大众应用。Se…

作者头像 李华