策略模式通过接口定义统一行为,如DiscountStrategy接口声明Calculate方法;具体策略如FixedDiscount、PercentDiscount等实现各自逻辑;PricingContext上下文持有策略并动态切换,避免冗长条件判断;结合map注册策略实例,支持按名称获取,便于扩展,符合开闭原则,提升代码可维护性与灵活性。

在Go语言中,策略模式能有效解耦算法实现与业务调用,特别适合处理多种相似但行为不同的业务场景。比如支付方式、折扣计算、数据导出格式等。通过接口定义统一行为,再由具体结构体实现不同逻辑,可以在运行时动态切换策略,避免大量if-else或switch判断,提升代码可维护性和扩展性。
定义策略接口
策略模式的核心是抽象出一个公共接口,声明所有支持的算法共有的操作。例如,处理不同类型的折扣:
type DiscountStrategy interface { Calculate(price float64) float64}
这个接口规定了所有折扣策略必须实现的方法。后续无论新增多少种折扣类型,只要实现该接口即可无缝接入。
实现具体策略
每种具体的折扣方式都对应一个结构体,并实现接口。比如满减、百分比折扣、固定金额减免:
立即学习“go语言免费学习笔记(深入)”;
type FixedDiscount struct { Amount float64}func (d *FixedDiscount) Calculate(price float64) float64 {return price - d.Amount}
type PercentDiscount struct {Rate float64}
func (d PercentDiscount) Calculate(price float64) float64 {return price (1 - d.Rate)}
type ThresholdDiscount struct {Threshold float64Amount float64}
func (d *ThresholdDiscount) Calculate(price float64) float64 {if price >= d.Threshold {return price - d.Amount}return price}
每个策略独立封装自己的计算逻辑,互不干扰,便于单元测试和复用。
上下文调用策略
创建一个上下文结构体来持有当前策略,并提供执行方法:
type PricingContext struct { strategy DiscountStrategy}func (c *PricingContext) SetStrategy(s DiscountStrategy) {c.strategy = s}
func (c *PricingContext) ApplyDiscount(price float64) float64 {if c.strategy == nil {return price}return c.strategy.Calculate(price)}
这样在业务中可以根据条件灵活更换策略:
context := &PricingContext{}context.SetStrategy(&PercentDiscount{Rate: 0.2})fmt.Println(context.ApplyDiscount(100)) // 输出 80
context.SetStrategy(&FixedDiscount{Amount: 15})fmt.Println(context.ApplyDiscount(100)) // 输出 85
不再需要写一堆条件判断,逻辑清晰且易于扩展。
结合配置或用户输入动态选择
实际项目中,策略的选择往往来自配置文件或API参数。可以使用map注册策略实例:
var strategies = map[string]DiscountStrategy{ "fixed": &FixedDiscount{Amount: 10}, "percent": &PercentDiscount{Rate: 0.1}, "threshold": &ThresholdDiscount{Threshold: 50, Amount: 5},}
然后根据传入的类型名获取对应策略:
func GetStrategy(name string) (DiscountStrategy, bool) { s, ok := strategies[name] return s, ok}
调用时只需传入策略标识即可自动匹配:
if strategy, ok := GetStrategy("percent"); ok { context.SetStrategy(strategy) result := context.ApplyDiscount(200)}
新增策略只需添加新结构体并注册到map中,完全符合开闭原则。
基本上就这些。用好策略模式能让Go项目的业务逻辑更干净,也更容易应对变化。
以上就是Golang如何使用策略模式优化业务逻辑的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1421039.html
微信扫一扫
支付宝扫一扫