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 天前
Go语言高并发实战:集成天远手机号码归属地核验API打造高性能风控中台
大数据·开发语言·后端·golang
范纹杉想快点毕业1 天前
嵌入式通信核心架构:从状态机、环形队列到多协议融合
linux·运维·c语言·算法·设计模式
__万波__1 天前
二十三种设计模式(二十)--解释器模式
java·设计模式·解释器模式
攀登的牵牛花1 天前
前端向架构突围系列 - 架构方法(一):概述 4+1 视图模型
前端·设计模式·架构
雲墨款哥1 天前
从一行好奇的代码说起:React的 useEffect 到底是不是生命周期?
前端·react.js·设计模式
源代码•宸1 天前
Leetcode—1161. 最大层内元素和【中等】
经验分享·算法·leetcode·golang
cultivator1294801 天前
设计原则和设计模式助记
设计模式
FreeBuf_1 天前
新型TCC绕过漏洞:macOS面临自动化攻击风险
macos·自动化·策略模式
enjoy编程1 天前
Spring boot 4 探究netty的关键知识点
spring boot·设计模式·reactor·netty·多线程
用户93816912553601 天前
Head First 单例模式
后端·设计模式