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)
}
相关推荐
XMYX-015 小时前
37 - Go env 环境变量:配置管理与运行时控制
开发语言·golang
小羊在睡觉19 小时前
力扣239. 滑动窗口最大值
数据结构·后端·算法·leetcode·go
一个在高校打杂的20 小时前
honeypot之opencanary(轻量化蜜罐)
linux·网络安全·网络攻击模型·安全威胁分析·策略模式
看山是山_Lau20 小时前
建造者模式:复杂对象如何一步步构建
设计模式·建造者模式
霸道流氓气质20 小时前
业务链路追踪日志设计模式 — 从原理到实践
设计模式
用户3983461612021 小时前
Go-Spring 实战第 7 课 —— Profile 多环境配置:基础配置与环境差异如何避免复制
spring·go
姚不倒21 小时前
Go 进阶实战:实现泛型数据验证器
云原生·golang
XMYX-01 天前
36 - Go exec 执行命令
开发语言·golang
lolo大魔王1 天前
Go 语言 HTTP 协议与 RESTful API 实训全解(理论 + 实战 + 规范)
http·golang·restful
审判长烧鸡1 天前
【Go Context】终极指南
go