go: Strategy Pattern

项目结构:

Go 复制代码
/*
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Strategy Pattern 策略模式
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : goLang 2024.3.6 go 26.2
# os        : windows 10
# database  : mysql 9.0 sql server 2019, postgreSQL 17.0  Oracle 21c Neo4j
# Datetime  : 2026/4/23 22:08
# User      :  geovindu
# Product   : GoLand
# Project   : godesginpattern
# File      : jewelry.go
*/
package entity
 
import "fmt"
 
// Jewelry 珠宝实体,不依赖任何包
type Jewelry struct {
    Material   string
    Weight     float64
    CraftLevel int
    Brand      string
}
 
func NewJewelry(material string, weight float64, craftLevel int, brand string) *Jewelry {
    return &Jewelry{
        Material:   material,
        Weight:     weight,
        CraftLevel: craftLevel,
        Brand:      brand,
    }
}
 
// ========== 新增:显示完整珠宝信息 ==========
func (j *Jewelry) ShowJewelryInfo() {
    fmt.Println("------------------------【珠宝基础信息】------------------------")
    fmt.Println("材质    : ", j.Material)
    fmt.Println("重量    : ", j.Weight, "克")
    fmt.Println("工艺等级: ", j.CraftLevel)
    fmt.Println("品牌    : ", j.Brand)
    fmt.Println("------------------------------------------------------------------------------------------")
}
Go 复制代码
/*
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Strategy Pattern 策略模式
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : goLang 2024.3.6 go 26.2
# os        : windows 10
# database  : mysql 9.0 sql server 2019, postgreSQL 17.0  Oracle 21c Neo4j
# Datetime  : 2026/4/23 22:09
# User      :  geovindu
# Product   : GoLand
# Project   : godesginpattern
# File      : pricing.go
*/
package strategy
 
import "godesginpattern/strategy/domain/entity"
 
// PricingStrategy 定价策略顶层接口
// 职责:定义统一契约,所有策略必须实现
type PricingStrategy interface {
    Calculate(j *entity.Jewelry) float64
}
Go 复制代码
/*
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Strategy Pattern 策略模式
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : goLang 2024.3.6 go 26.2
# os        : windows 10
# database  : mysql 9.0 sql server 2019, postgreSQL 17.0  Oracle 21c Neo4j
# Datetime  : 2026/4/23 22:09
# User      :  geovindu
# Product   : GoLand
# Project   : godesginpattern
# File      : brand.go
*/
package strategy
 
import "godesginpattern/strategy/domain/entity"
 
// BrandPremiumPricing 品牌溢价定价策略
type BrandPricing struct{}
 
func (b *BrandPricing) Calculate(j *entity.Jewelry) float64 {
    base := j.Weight * 100
    switch j.Brand {
    case "高端品牌":
        return base * 3
    default:
        return base
    }
}
 
 
/*
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Strategy Pattern 策略模式
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : goLang 2024.3.6 go 26.2
# os        : windows 10
# database  : mysql 9.0 sql server 2019, postgreSQL 17.0  Oracle 21c Neo4j
# Datetime  : 2026/4/23 22:09
# User      :  geovindu
# Product   : GoLand
# Project   : godesginpattern
# File      : craft.go
*/
package strategy
 
import "godesginpattern/strategy/domain/entity"
 
// CraftPricing 按工艺定价策略
type CraftPricing struct{}
 
func (c *CraftPricing) Calculate(j *entity.Jewelry) float64 {
    return 1000 + float64(j.CraftLevel)*500
}
 
 
/*
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Strategy Pattern 策略模式
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : goLang 2024.3.6 go 26.2
# os        : windows 10
# database  : mysql 9.0 sql server 2019, postgreSQL 17.0  Oracle 21c Neo4j
# Datetime  : 2026/4/23 22:09
# User      :  geovindu
# Product   : GoLand
# Project   : godesginpattern
# File      : weight.go
*/
package strategy
 
import "godesginpattern/strategy/domain/entity"
 
// WeightPricing 按重量定价策略
// 职责:仅实现重量计价算法,无任何其他逻辑
type WeightPricing struct{}
 
func (w *WeightPricing) Calculate(j *entity.Jewelry) float64 {
    var unitPrice float64
    switch j.Material {
    case "黄金":
        unitPrice = 500
    case "白银":
        unitPrice = 10
    default:
        unitPrice = 50
    }
    return unitPrice * j.Weight
}
Go 复制代码
/*
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Strategy Pattern 策略模式
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : goLang 2024.3.6 go 26.2
# os        : windows 10
# database  : mysql 9.0 sql server 2019, postgreSQL 17.0  Oracle 21c Neo4j
# Datetime  : 2026/4/23 22:20
# User      :  geovindu
# Product   : GoLand
# Project   : godesginpattern
# File      : jewelry_context.go
*/
package context
 
import (
    "godesginpattern/strategy/domain/entity"
    "godesginpattern/strategy/domain/strategy"
)
 
// JewelryContext 上下文:持有实体 + 策略
// 这是策略模式标准 Context
type JewelryContext struct {
    jewelry  *entity.Jewelry
    strategy strategy.PricingStrategy
}
 
func NewJewelryContext(j *entity.Jewelry) *JewelryContext {
    return &JewelryContext{jewelry: j}
}
 
// SetStrategy 设置策略
func (c *JewelryContext) SetStrategy(s strategy.PricingStrategy) {
    c.strategy = s
}
 
// CalculatePrice 计算价格
func (c *JewelryContext) CalculatePrice() float64 {
    return c.strategy.Calculate(c.jewelry)
}

调用:

Go 复制代码
/*
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Strategy Pattern 策略模式
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : goLang 2024.3.6 go 26.2
# os        : windows 10
# database  : mysql 9.0 sql server 2019, postgreSQL 17.0  Oracle 21c Neo4j
# Datetime  : 2026/4/23 22:22
# User      :  geovindu
# Product   : GoLand
# Project   : godesginpattern
# File      : strategybll.go
*/
package bll
 
import (
    "fmt"
    "godesginpattern/strategy/context"
    "godesginpattern/strategy/domain/entity"
    "godesginpattern/strategy/strategy"
)
 
func StrategyMain() {
    // 创建珠宝
    j := entity.NewJewelry("黄金", 10, 4, "高端品牌")
 
    j.ShowJewelryInfo()
    // 创建上下文
    ctx := context.NewJewelryContext(j)
 
    // 重量策略
    ctx.SetStrategy(&strategy.WeightPricing{})
    fmt.Printf("重量计价:%.2f\n", ctx.CalculatePrice())
 
    // 工艺策略
    ctx.SetStrategy(&strategy.CraftPricing{})
    fmt.Printf("工艺计价:%.2f\n", ctx.CalculatePrice())
 
    // 品牌策略
    ctx.SetStrategy(&strategy.BrandPricing{})
    fmt.Printf("品牌计价:%.2f\n", ctx.CalculatePrice())
 
    j = entity.NewJewelry("蓝宝石", 20, 5, "高端品牌")
    j.ShowJewelryInfo()
    // 创建上下文
    ctx = context.NewJewelryContext(j)
 
    // 重量策略
    ctx.SetStrategy(&strategy.WeightPricing{})
    fmt.Printf("重量计价:%.2f\n", ctx.CalculatePrice())
 
    // 工艺策略
    ctx.SetStrategy(&strategy.CraftPricing{})
    fmt.Printf("工艺计价:%.2f\n", ctx.CalculatePrice())
 
    // 品牌策略
    ctx.SetStrategy(&strategy.BrandPricing{})
    fmt.Printf("品牌计价:%.2f\n", ctx.CalculatePrice())
     
}

输出:

相关推荐
27669582921 小时前
阿里最新acw_sc__v2 分析
开发语言·python·acw_sc__v2·acw_sc__v2逆向·acw_sc__v2算法·acw_sc__v2算法分析·cookie逆向
dog2501 小时前
圆锥曲线和二次曲线
开发语言·网络·人工智能·算法·php
AI人工智能+电脑小能手1 小时前
【大白话说Java面试题】【Java基础篇】第25题:JDK1.8的新特性有哪些
java·开发语言·后端·面试
开发小程序的之朴1 小时前
基于Go语言的企业级CMS系统架构设计与性能分析——以AnQiCMS为例
开发语言·golang·系统架构
叶小鸡2 小时前
Java 篇-项目实战-天机学堂(从0到1)-day9
java·开发语言
小短腿的代码世界2 小时前
Qt券商接口封装深度解析:统一API设计与多源适配
开发语言·qt·单元测试
wearegogog1232 小时前
基于Q-learning的栅格地图路径规划MATLAB仿真程序
开发语言·算法·matlab
csbysj20202 小时前
Java 条件语句
开发语言
Ulyanov3 小时前
《现代 Python 桌面应用架构实战:PySide6 + QML 从入门到工程化》 开发环境搭建与工具链极简主义 —— 拒绝臃肿,构建工业级基座
开发语言·python·qt·ui·架构·系统仿真