惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园_首页
PCI Perspectives
PCI Perspectives
H
Help Net Security
爱范儿
爱范儿
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
雷峰网
雷峰网
S
Secure Thoughts
Jina AI
Jina AI
Attack and Defense Labs
Attack and Defense Labs
大猫的无限游戏
大猫的无限游戏
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
S
Security @ Cisco Blogs
阮一峰的网络日志
阮一峰的网络日志
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Recent Commits to openclaw:main
Recent Commits to openclaw:main
The Hacker News
The Hacker News
The Last Watchdog
The Last Watchdog
T
Tor Project blog
小众软件
小众软件
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 叶小钗
博客园 - 【当耐特】
G
Google Developers Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
C
Cyber Attacks, Cyber Crime and Cyber Security
C
Cisco Blogs
T
The Blog of Author Tim Ferriss
I
Intezer
S
Schneier on Security
S
Securelist
W
WeLiveSecurity
C
Cybersecurity and Infrastructure Security Agency CISA
P
Palo Alto Networks Blog
Scott Helme
Scott Helme
Project Zero
Project Zero
Google Online Security Blog
Google Online Security Blog
T
Threatpost
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Engineering at Meta
Engineering at Meta
Blog — PlanetScale
Blog — PlanetScale
V
Visual Studio Blog
Last Week in AI
Last Week in AI
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
人人都是产品经理
人人都是产品经理
Y
Y Combinator Blog
A
Arctic Wolf
GbyAI
GbyAI
H
Hackread – Cybersecurity News, Data Breaches, AI and More
L
LangChain Blog

博客园 - ®Geovin Du Dream Park™

go: Functional Options Pattern go:Timing Functions Pattern python: Timing Functions Pattern go: Steady-State Pattern python: Steady-State Pattern go: Handshaking Pattern python: Handshaking Pattern go: Fail-Fast Pattern python: Fail-Fast Pattern I go: Deadline Pattern python: Deadline Pattern go: Circuit-Breaker Pattern python: Circuit-Breaker Pattern go: Bulkheads Pattern python: Bulkheads Pattern go: Push & Pull Pattern python: Push & Pull Pattern python: Publish/Subscribe Pattern II go: Publish/Subscribe Pattern python: Publish/Subscribe Pattern go: Futures & Promises Pattern python: Futures & Promises Pattern go: Worker Pool Pattern go:Pipeline Pattern python: Worker Pool Pattern python: Pipeline Pattern go: Fan-Out Pattern python: Fan-Out Pattern go: Fan-In Pattern python: Fan-In Pattern Fan-In go: Producer Consumer  Pattern python: Producer Consumer Pattern go: Parallelism Pattern python: Parallelism Pattern go: Reactor Pattern python: Reactor Pattern go: Generators Pattern python: speech to text offline python: Generators Pattern go: Coroutines Pattern python:Coroutines Pattern go: Broadcast Pattern python: Broadcast Pattern python: Bounded Parallelism Pattern go: Bounded Parallelism Pattern python: N-Barrier Pattern go: N-Barrier Pattern python: Semaphore Pattern go: Semaphore Pattern python: Read-Write Lock Pattern go: Read-Write Lock Pattern python: Monitor Pattern go: Monitor Pattern python: Mutex Pattern go: Lock/Mutex Pattern Python: Condition Variable Pattern go:Condition Variable Pattern python: Registry Pattern python: Interpreter Pattern go: Interpreter Pattern go: Registry Pattern go: Memento Pattern go: Command Pattern go: State Pattern go:Template Method Pattern go: Visitor Pattern go: Observer Pattern go: Mediator Pattern go: Iterator Pattern go: Chain of Responsibility Pattern go: Proxy Pattern go:Decorator Pattern go: Facade Pattern go: Flyweight Pattern go: Composite Pattern go: Singleton Pattern go: Prototype Pattern go: Bridge Pattern go: Adapter Pattern go: Builder Pattern 密码进行加盐哈希 using CSharp,Python,Go,Java go: Abstract Factory Pattern go: Model,Interface,DAL ,Factory,BLL using mysql go: Simple Factory Pattern go: Factory Method Pattern go: goLang 在Windows环境搭建Go语言开发环境 CSharp: Parallel Extensions cpp: class python: 蝴蝶跟随鼠标 javascript: 中国历史人物热力分布图using echart javascript: 中国历史人物热力图 python: 初养龙虾微信纯文字自动回复using workBuddy python: object 入门 python: Factory Method Pattern python: Simple Factory Pattern python: Abstract Factory Pattern 区域化 代码 python: Null Object Pattern python: Builder Pattern python: Adapter Pattern
go: Strategy Pattern
®Geovin Du Dream Park™ · 2026-04-23 · via 博客园 - ®Geovin Du Dream Park™

项目结构:

image

/*
# 版权所有  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("——————————————————————————————")
}
/*
# 版权所有  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
}
/*
# 版权所有  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
}
/*
# 版权所有  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)
}

调用:

/*
# 版权所有  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())
	
}

输出:

image

哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)