GO设计模式——24、策略模式(行为型)

目录

[策略模式(Strategy Pattern)](#策略模式(Strategy Pattern))

策略模式的核心角色:

优缺点

使用场景

注意事项

代码实现


策略模式(Strategy Pattern)

策略模式(Strategy Pattern)允许在运行时动态地改变算法或策略的选择,从而使算法的变化独立于使用它的客户端。这种模式通过将算法封装在单独的类中,使得它们可以相互替换,并使得它们易于理解、扩展和维护。

策略模式核心角色

  • 环境(Context):定义客户端所感兴趣的接口,并维护一个具体策略的实例。
  • 抽象策略(Strategy):定义一个接口,用于封装具体策略的行为。
  • 具体策略(Concrete Strategy):实现抽象策略定义的接口,完成具体策略对应的行为。

优缺点

(1)优点:

  • 算法可以自由切换。
  • 避免使用多重条件判断。
  • 扩展性良好。

(2)缺点:

  • 策略类会增多。
  • 所有策略类都需要对外暴露。

使用场景

  • 如果在一个系统里面有许多类,它们之间的区别仅在于它们的行为,那么使用策略模式可以动态地让一个对象在许多行为中选择一种行为。
  • 一个系统需要动态地在几种算法中选择一种。
  • 如果一个对象有很多的行为,如果不用恰当的模式,这些行为就只好使用多重的条件选择语句来实现。

注意事项

  • 如果一个系统的策略多于四个,就需要考虑使用混合模式,解决策略类膨胀的问题。

代码实现

Go 复制代码
package main

import "fmt"

// 电商系统,针对不同的用户类型(普通用户、VIP用户、超级VIP用户),希望根据用户类型来计算商品的折扣价格。

// 环境接口
type PricingContext interface {
    SetStrategy(strategy PricingStrategy)
    CalculatePrice(originalPrice float64) float64
}

// 抽象策略接口
type PricingStrategy interface {
    CalculatePrice(originalPrice float64) float64
}

// 具体策略:普通用户策略
type RegularUserStrategy struct{}

func (s *RegularUserStrategy) CalculatePrice(originalPrice float64) float64 {
    return originalPrice
}

// 具体策略:VIP用户策略
type VipUserStrategy struct{}

func (s *VipUserStrategy) CalculatePrice(originalPrice float64) float64 {
    return originalPrice * 0.9
}

// 具体策略:超级VIP用户策略
type SuperVipUserStrategy struct{}

func (s *SuperVipUserStrategy) CalculatePrice(originalPrice float64) float64 {
    return originalPrice * 0.8
}

// 具体环境
type PricingService struct {
    strategy PricingStrategy
}

func (s *PricingService) SetStrategy(strategy PricingStrategy) {
    s.strategy = strategy
}

func (s *PricingService) CalculatePrice(originalPrice float64) float64 {
    return s.strategy.CalculatePrice(originalPrice)
}

// 客户端代码
func main() {
    pricingService := &PricingService{}
    pricingService.SetStrategy(&RegularUserStrategy{})

    originalPrice := 100.0
    discountedPrice := pricingService.CalculatePrice(originalPrice)
    fmt.Printf("普通用户原价:%v,折扣价:%v\n", originalPrice, discountedPrice)

    pricingService.SetStrategy(&VipUserStrategy{})
    discountedPrice = pricingService.CalculatePrice(originalPrice)
    fmt.Printf("VIP用户原价:%v,折扣价:%v\n", originalPrice, discountedPrice)

    pricingService.SetStrategy(&SuperVipUserStrategy{})
    discountedPrice = pricingService.CalculatePrice(originalPrice)
    fmt.Printf("超级VIP用户原价:%v,折扣价:%v\n", originalPrice, discountedPrice)
}
相关推荐
程序员爱钓鱼5 小时前
Go语言实战案例——进阶与部署篇:编写Makefile自动构建Go项目
后端·算法·go
该用户已不存在6 小时前
别再用 if err != nil 了,学会这几个技巧,假装自己是Go大神
后端·go
李广坤7 小时前
状态模式(State Pattern)
设计模式
n8n8 小时前
Go语言操作Redis全面指南
go
李广坤8 小时前
观察者模式(Observer Pattern)
设计模式
李广坤9 小时前
中介者模式(Mediator Pattern)
设计模式
李广坤9 小时前
迭代器模式(Iterator Pattern)
设计模式
李广坤10 小时前
解释器模式(Interpreter Pattern)
设计模式
会跑的葫芦怪10 小时前
Go语言操作Redis
开发语言·redis·golang
静谧之心12 小时前
在 K8s 上可靠运行 PD 分离推理:RBG 的设计与实现
云原生·容器·golang·kubernetes·开源·pd分离