Golang:设计模式之策略模式

小小的需求

获取一个csgo饰品的定价,需要从多个渠道(steam、buff、igxe......)等等。

需求很简单,下班前搞定吧。

开工

1、先定义一个传输对象:dto.go

Go 复制代码
package price

type GetPriceReq struct {
	GoodsId int64 `json:"goods_id"`
}

2、具体实现:interface.go

Go 复制代码
package price

import (
	"context"
	"strings"
)

// GoodsStrategyPrice 饰品定价策略
type GoodsStrategyPrice interface {
	// GetPrice 根据饰品ID获取系统自定义价格
	GetPrice(ctx context.Context, req *GetPriceReq) int64
}

// GoodsStrategy 用于选择和执行策略
type GoodsStrategy struct {
	strategy GoodsStrategyPrice
}

// SetStrategy 选择策略
func (s *GoodsStrategy) SetStrategy(strategy GoodsStrategyPrice) {
	s.strategy = strategy
}

// GetPrice 执行策略
func (s *GoodsStrategy) GetPrice(ctx context.Context, req *GetPriceReq) int64 {
	return s.strategy.GetPrice(ctx, req)
}

// GetPriceByGoods 根据策略获取价格
func GetPriceByGoods(ctx context.Context, req *GetPriceReq, strategy string) int64 {
	// 策略
	var strategyObj GoodsStrategyPrice

	// 去除空格、转小写
	strategy = strings.ToLower(strings.TrimSpace(strategy))

	switch strategy {
	case "steam":
		strategyObj = NewGoodsSteamPrice()
	case "buff":
		strategyObj = NewGoodsBuffPrice()
	case "igxe":
		strategyObj = NewGoodsIgxePrice()
	default: // 默认策略
		strategyObj = NewGoodsSteamPrice()
	}

	obj := &GoodsStrategy{}
	obj.SetStrategy(strategyObj)

	return obj.GetPrice(ctx, req)
}

// GoodsSteamPrice Steam定价
type GoodsSteamPrice struct{}

// NewGoodsSteamPrice 定价策略:Steam定价
func NewGoodsSteamPrice() *GoodsSteamPrice {
	return &GoodsSteamPrice{}
}

// GetPrice 根据饰品ID获取系统自定义价格:Steam定价
func (p *GoodsSteamPrice) GetPrice(ctx context.Context, req *GetPriceReq) int64 {
	return 100
}

// GoodsBuffPrice Buff定价
type GoodsBuffPrice struct{}

// NewGoodsBuffPrice 定价策略: Buff定价
func NewGoodsBuffPrice() *GoodsBuffPrice {
	return &GoodsBuffPrice{}
}

// GetPrice 根据饰品ID获取系统自定义价格:Buff定价
func (p *GoodsBuffPrice) GetPrice(ctx context.Context, req *GetPriceReq) int64 {
	return 200
}

// GoodsIgxePrice Igxe定价
type GoodsIgxePrice struct{}

// NewGoodsIgxePrice 定价策略: Igxe定价
func NewGoodsIgxePrice() *GoodsIgxePrice {
	return &GoodsIgxePrice{}
}

// GetPrice 根据饰品ID获取系统自定义价格:Igxe定价
func (p *GoodsIgxePrice) GetPrice(ctx context.Context, req *GetPriceReq) int64 {
	return 300
}
运行一下

是骡子是马,拉出来溜溜

Go 复制代码
package main

import (
	"fmt"
	"strategy/price"
)

func main() {

	strategy := "steam"

	priceData := price.GetPriceByGoods(nil, &price.GetPriceReq{GoodsId: 1}, strategy)

	fmt.Printf("策略:%s 定价:%d\n", strategy, priceData)
}

输出结果:

一气呵成,还真能运行起来,这你受得了嘛


解释一下

这段代码展示了策略模式(Strategy Pattern)的设计思想。策略模式是一种行为设计模式,它使你能在运行时改变对象的行为。

以下是策略模式的关键组件在代码中的体现:

  1. **策略接口 (GoodsStrategyPrice):**定义了所有支持的策略或行为的公共接口。在这个例子中,接口声明了一个方法 GetPrice,用于根据饰品ID获取系统自定义价格。
  2. **具体策略类 (GoodsSteamPrice, GoodsBuffPrice, GoodsIgxePrice):**这些类实现了GoodsStrategyPrice接口,每种都代表了一种具体的定价策略。例如,GoodsSteamPrice 类的 GetPrice 方法返回100,模拟了Steam平台的定价规则。
  3. **上下文类 (GoodsStrategy):**此类用来封装使用哪种策略的上下文,并且提供了设置策略(SetStrategy)和执行策略(GetPrice)的方法。它持有一个GoodsStrategyPrice类型的策略对象,使得在运行时可以动态切换策略。
  4. **策略选择函数 (GetPriceByGoods):**这个函数根据传入的策略字符串参数,选择并实例化相应的策略对象,然后通过GoodsStrategy来执行选定的策略,获取价格。

通过这种方式,该设计允许程序在运行时选择不同的算法或策略来处理同一类型的问题,而无需知道具体策略是如何实现的,提高了代码的灵活性和可扩展性。如果未来需要添加新的定价策略,只需实现GoodsStrategyPrice接口并相应地修改策略选择逻辑即可。



我为人人,人人为我,美美与共,天下大同。

相关推荐
静水流深_沧海一粟4 小时前
04 | 别再写几十个参数的构造函数了——建造者模式
设计模式
StarkCoder4 小时前
从UIKit到SwiftUI的迁移感悟:数据驱动的革命
设计模式
阿星AI工作室11 小时前
给openclaw龙虾造了间像素办公室!实时看它写代码、摸鱼、修bug、写日报,太可爱了吧!
前端·人工智能·设计模式
_哆啦A梦1 天前
Vibe Coding 全栈专业名词清单|设计模式·基础篇(创建型+结构型核心名词)
前端·设计模式·vibecoding
阿闽ooo4 天前
中介者模式打造多人聊天室系统
c++·设计模式·中介者模式
小米4964 天前
js设计模式 --- 工厂模式
设计模式
逆境不可逃5 天前
【从零入门23种设计模式08】结构型之组合模式(含电商业务场景)
线性代数·算法·设计模式·职场和发展·矩阵·组合模式
驴儿响叮当20105 天前
设计模式之状态模式
设计模式·状态模式
电子科技圈5 天前
XMOS推动智能音频等媒体处理技术从嵌入式系统转向全新边缘计算
人工智能·mcu·物联网·设计模式·音视频·边缘计算·iot
徐先生 @_@|||5 天前
安装依赖三方exe/msi的软件设计模式
设计模式