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)
}
相关推荐
你的人类朋友1 天前
设计模式的原则有哪些?
前端·后端·设计模式
九江Mgx1 天前
用 Go 手搓一个内网 DNS 服务器:从此告别 IP 地址,用域名畅游家庭网络!
golang·dns服务·内网dns
YA3331 天前
java设计模式八、组合模式
java·设计模式·组合模式
Yeats_Liao1 天前
Go Web 编程快速入门 12 - 微服务架构:服务发现、负载均衡与分布式系统
前端·后端·架构·golang
Yeniden1 天前
设计模式>原型模式大白话讲解:就像复印机,拿个原件一复印,就得到一模一样的新东西
java·设计模式·原型模式·1024程序员节
成钰1 天前
设计模式简介
设计模式
成钰1 天前
设计模式之抽象工厂模式:最复杂的工厂模式变种
java·设计模式·抽象工厂模式
Asort1 天前
JavaScript设计模式(二十三)——访问者模式:优雅地扩展对象结构
前端·javascript·设计模式
cipher1 天前
用 Go 找预测市场的赚钱机会!
后端·go·web3
xiaoye37081 天前
23种设计模式之策略模式
设计模式·策略模式