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)
}
相关推荐
青春易逝丶18 小时前
策略模式
java·开发语言·策略模式
Yu_Lijing20 小时前
基于C++的《Head First设计模式》笔记——生成器模式
c++·笔记·设计模式
古城小栈20 小时前
Go 底层代码的完整分类
开发语言·后端·golang
耳冉鹅20 小时前
Go无锁共享内存环形缓冲区设计
开发语言·golang
程序员爱钓鱼21 小时前
Go PDF处理利器: github.com/pdfcpu/pdfcpu 深度指南
后端·面试·go
sg_knight21 小时前
设计模式实战:策略模式(Strategy)
java·开发语言·python·设计模式·重构·架构·策略模式
江湖十年21 小时前
使用 testing/synctest 测试并发代码
后端·面试·go
liangshanbo121521 小时前
[特殊字符] macOS 上的 zoxide:智能目录跳转终极指南
macos·策略模式
吐个泡泡v1 天前
Python 开发“设计模式”指南
python·设计模式