策略模式通过接口抽象行为,用结构体实现不同策略,借助上下文动态切换,避免冗长条件判断。定义DiscountStrategy接口及多种折扣实现,如NormalDiscount、VIPDiscount等,再通过PriceCalculator持有策略并执行计算。根据用户类型从预注册的map中获取对应策略,实现灵活扩展与开闭原则,提升代码可维护性。

在 Go 语言中,策略模式能有效替代冗长的 if-else 或 switch-case 条件判断,提升代码的可维护性和扩展性。当你有一组相似行为、需要根据不同类型动态选择执行方式时,策略模式是一个理想选择。
定义统一的行为接口
策略模式的核心是抽象出一个公共接口,所有具体策略都实现这个接口。例如,假设我们要根据不同的折扣类型计算价格:
type DiscountStrategy interface { Apply(price float64) float64}
实现多种具体策略
每种折扣方式作为一个独立结构体实现接口,比如普通会员、VIP 会员、超级 VIP 折扣:
type NormalDiscount struct{}func (d NormalDiscount) Apply(price float64) float64 {return price 0.95 // 95折}
type VIPDiscount struct{}
立即学习“go语言免费学习笔记(深入)”;
func (d VIPDiscount) Apply(price float64) float64 {return price 0.9 // 9折}
type SuperVIPDiscount struct{}
func (d SuperVIPDiscount) Apply(price float64) float64 {return price 0.8 // 8折}
使用策略上下文动态切换逻辑
创建一个上下文结构体来持有当前策略,并提供设置和执行方法:
type PriceCalculator struct { strategy DiscountStrategy}func (c *PriceCalculator) SetStrategy(s DiscountStrategy) {c.strategy = s}
func (c *PriceCalculator) Calculate(price float64) float64 {if c.strategy == nil {panic("未设置策略")}return c.strategy.Apply(price)}
调用时根据用户类型切换策略,不再使用条件判断:
calculator := &PriceCalculator{}// 模拟不同用户var strategy DiscountStrategyswitch userType {case "normal":strategy = &NormalDiscount{}case "vip":strategy = &VIPDiscount{}case "super_vip":strategy = &SuperVIPDiscount{}default:strategy = &NormalDiscount{}}
calculator.SetStrategy(strategy)finalPrice := calculator.Calculate(100)
更进一步,可以将类型到策略的映射预先注册,彻底消除条件分支:
var strategies = map[string]DiscountStrategy{ "normal": &NormalDiscount{}, "vip": &VIPDiscount{}, "super_vip": &SuperVIPDiscount{},}// 使用时直接获取if strategy, ok := strategies[userType]; ok {calculator.SetStrategy(strategy)}
这样,新增折扣类型只需添加新结构体并注册到 map,无需修改已有逻辑,符合开闭原则。
基本上就这些。用接口抽象行为,通过注入不同实现来替换条件判断,Go 虽无继承,但组合和接口让策略模式依然简洁有力。关键是避免在业务中散落一堆 if-else,把变化封装起来。
以上就是Golang如何实现策略模式替换条件逻辑的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1414275.html
微信扫一扫
支付宝扫一扫