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

推荐订阅源

C
Check Point Blog
GbyAI
GbyAI
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
U
Unit 42
美团技术团队
NISL@THU
NISL@THU
C
Cisco Blogs
SecWiki News
SecWiki News
N
Netflix TechBlog - Medium
Forbes - Security
Forbes - Security
Cloudbric
Cloudbric
雷峰网
雷峰网
T
Tailwind CSS Blog
博客园 - 司徒正美
The Register - Security
The Register - Security
L
LangChain Blog
S
Security Affairs
Hacker News - Newest:
Hacker News - Newest: "LLM"
B
Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
T
Threat Research - Cisco Blogs
I
InfoQ
S
Schneier on Security
L
Lohrmann on Cybersecurity
量子位
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Martin Fowler
Martin Fowler
Schneier on Security
Schneier on Security
F
Fortinet All Blogs
TaoSecurity Blog
TaoSecurity Blog
K
Kaspersky official blog
Google DeepMind News
Google DeepMind News
Cisco Talos Blog
Cisco Talos Blog
PCI Perspectives
PCI Perspectives
Attack and Defense Labs
Attack and Defense Labs
WordPress大学
WordPress大学
Microsoft Azure Blog
Microsoft Azure Blog
H
Help Net Security
Project Zero
Project Zero
The GitHub Blog
The GitHub Blog
D
Docker
N
News | PayPal Newsroom
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
H
Hacker News: Front Page
云风的 BLOG
云风的 BLOG
Microsoft Security Blog
Microsoft Security Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 聂微东
Webroot Blog
Webroot Blog
MongoDB | Blog
MongoDB | 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: Strategy Pattern go: Visitor Pattern go: Observer Pattern go: Mediator Pattern go: Iterator Pattern go: Chain of Responsibility Pattern go: Proxy 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:Decorator Pattern
®Geovin Du D · 2026-04-20 · via 博客园 - ®Geovin Du Dream Park™

项目结构:

image

/*
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Decorator 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/20 20:07
# User      :  geovindu
# Product   : GoLand
# Project   : godesginpattern
# File      : decorator.go
*/
package domain

// BaseDecorator 装饰器基类(组合模式:持有Jewelry接口)
// 所有增值装饰器都继承它,实现默认转发
type BaseDecorator struct {
	Jewelry Jewelry
}

// 默认转发:不修改原属性/流程,子类重写实现增强
func (d *BaseDecorator) GetAttribute() JewelryAttribute {
	return d.Jewelry.GetAttribute()
}

func (d *BaseDecorator) GetProcessList() []ProcessStep {
	return d.Jewelry.GetProcessList()
}

func (d *BaseDecorator) GetProductName() string {
	return d.Jewelry.GetProductName()
}


/*
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Decorator 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/20 20:07
# User      :  geovindu
# Product   : GoLand
# Project   : godesginpattern
# File      : jewelry.go
*/
package domain

// JewelryAttribute 珠宝结构化属性(企业级强类型,禁止字符串拼接)
type JewelryAttribute struct {
	Material string `json:"material"` // 材质
	Craft    string `json:"craft"`    // 工艺
	Inlay    string `json:"inlay"`    // 镶嵌(增值)
	Cert     string `json:"cert"`     // 证书(增值)
	Pack     string `json:"pack"`     // 包装(增值)
}

// ProcessStep 加工流程步骤(结构化流程)
type ProcessStep struct {
	StepName string `json:"step_name"`
	Sequence int    `json:"sequence"` // 执行顺序
}

// Jewelry 珠宝标准接口(企业契约:固定不变,统一规范)
type Jewelry interface {
	// GetAttribute 获取完整珠宝属性(结构化)
	GetAttribute() JewelryAttribute
	// GetProcessList 获取完整流程链(结构化)
	GetProcessList() []ProcessStep
	// GetProductName 获取产品名称
	GetProductName() string
}
/*
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Decorator 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/20 20:07
# User      :  geovindu
# Product   : GoLand
# Project   : godesginpattern
# File      : basic_jewelry.go
*/
package service

import "godesginpattern/decorator/domain"

// BasicJewelry 基础珠宝实现(核心实体:材质+基础工艺+基础流程)
type BasicJewelry struct {
	attribute domain.JewelryAttribute
}

// NewBasicJewelry 创建基础珠宝(工厂模式)
func NewBasicJewelry(material, craft string) domain.Jewelry {
	return &BasicJewelry{
		attribute: domain.JewelryAttribute{
			Material: material,
			Craft:    craft,
		},
	}
}

// GetAttribute 实现接口:返回核心属性
func (b *BasicJewelry) GetAttribute() domain.JewelryAttribute {
	return b.attribute
}

// GetProcessList 实现接口:返回基础加工流程
func (b *BasicJewelry) GetProcessList() []domain.ProcessStep {
	return []domain.ProcessStep{
		{StepName: "原料检验", Sequence: 1},
		{StepName: "切割成型", Sequence: 2},
		{StepName: "基础抛光", Sequence: 3},
	}
}

// GetProductName 产品名称
func (b *BasicJewelry) GetProductName() string {
	return "基础珠宝"
}
/*
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Decorator 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/20 20:07
# User      :  geovindu
# Product   : GoLand
# Project   : godesginpattern
# File      : inlay.go
*/
package decorator

import "godesginpattern/decorator/domain"

// DiamondInlayDecorator 钻石镶嵌装饰器
type DiamondInlayDecorator struct {
	domain.BaseDecorator
}

func NewDiamondInlay(j domain.Jewelry) domain.Jewelry {
	return &DiamondInlayDecorator{
		BaseDecorator: domain.BaseDecorator{Jewelry: j},
	}
}

// 重写:扩展属性
func (d *DiamondInlayDecorator) GetAttribute() domain.JewelryAttribute {
	attr := d.Jewelry.GetAttribute()
	attr.Inlay = "天然钻石镶嵌"
	return attr
}

// 重写:扩展流程
func (d *DiamondInlayDecorator) GetProcessList() []domain.ProcessStep {
	list := d.Jewelry.GetProcessList()
	nextSeq := len(list) + 1
	return append(list,
		domain.ProcessStep{StepName: "钻石镶嵌", Sequence: nextSeq},
		domain.ProcessStep{StepName: "镶嵌精度质检", Sequence: nextSeq + 1},
	)
}

func (d *DiamondInlayDecorator) GetProductName() string {
	return d.Jewelry.GetProductName() + " + 钻石镶嵌款"
}


/*
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Decorator 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/20 20:08
# User      :  geovindu
# Product   : GoLand
# Project   : godesginpattern
# File      : certificate.go
*/
package decorator

import "godesginpattern/decorator/domain"

// GIACertificateDecorator GIA证书装饰器
type GIACertificateDecorator struct {
	domain.BaseDecorator
}

func NewGIACertificate(j domain.Jewelry) domain.Jewelry {
	return &GIACertificateDecorator{
		BaseDecorator: domain.BaseDecorator{Jewelry: j},
	}
}

func (g *GIACertificateDecorator) GetAttribute() domain.JewelryAttribute {
	attr := g.Jewelry.GetAttribute()
	attr.Cert = "GIA国际权威证书"
	return attr
}

func (g *GIACertificateDecorator) GetProcessList() []domain.ProcessStep {
	list := g.Jewelry.GetProcessList()
	nextSeq := len(list) + 1
	return append(list,
		domain.ProcessStep{StepName: "权威鉴定", Sequence: nextSeq},
		domain.ProcessStep{StepName: "证书出具", Sequence: nextSeq + 1},
	)
}

func (g *GIACertificateDecorator) GetProductName() string {
	return g.Jewelry.GetProductName() + " + 带GIA证书"
}


/*
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Decorator 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/20 20:08
# User      :  geovindu
# Product   : GoLand
# Project   : godesginpattern
# File      : package.go
*/
package decorator

import "godesginpattern/decorator/domain"

// LuxuryPackDecorator 高端包装装饰器
type LuxuryPackDecorator struct {
	domain.BaseDecorator
}

func NewLuxuryPack(j domain.Jewelry) domain.Jewelry {
	return &LuxuryPackDecorator{
		BaseDecorator: domain.BaseDecorator{Jewelry: j},
	}
}

func (l *LuxuryPackDecorator) GetAttribute() domain.JewelryAttribute {
	attr := l.Jewelry.GetAttribute()
	attr.Pack = "高端红木礼盒+防伪包装"
	return attr
}

func (l *LuxuryPackDecorator) GetProcessList() []domain.ProcessStep {
	list := l.Jewelry.GetProcessList()
	nextSeq := len(list) + 1
	return append(list,
		domain.ProcessStep{StepName: "精包装", Sequence: nextSeq},
		domain.ProcessStep{StepName: "成品终检", Sequence: nextSeq + 1},
	)
}

func (l *LuxuryPackDecorator) GetProductName() string {
	return l.Jewelry.GetProductName() + " + 高端礼盒"
}

调用:

/*
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Decorator 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/20 20:12
# User      :  geovindu
# Product   : GoLand
# Project   : godesginpattern
# File      : decoratorbll.go
Decorator
├── domain/         领域层:核心实体、接口、常量(业务本源)
│   ├── jewelry.go  珠宝实体、属性结构、流程结构
│   └── decorator.go 装饰器接口定义
├── service/        服务层:基础服务实现
│   └── basic_jewelry.go
├── decorator/      装饰器层:增值扩展实现(属性+流程)
│   ├── inlay.go
│   ├── certificate.go
│   └── package.go
└── main.go         入口层:业务编排
*/
package bll

import (
	"encoding/json"
	"fmt"
	"godesginpattern/decorator/decorator"
	"godesginpattern/decorator/service"
)

func DecoratorMain() {
	// 1. 创建基础珠宝
	base := service.NewBasicJewelry("18K金", "5A级镜面抛光")

	// 2. 链式装饰(企业级灵活编排:顺序随意、可插拔)
	final := decorator.NewLuxuryPack(
		decorator.NewGIACertificate(
			decorator.NewDiamondInlay(base),
		),
	)

	// 3. 输出结构化结果(可直接用于API/数据库/日志)
	fmt.Println("=== 最终珠宝产品 ===")
	fmt.Println("产品名称:", final.GetProductName())

	attr, _ := json.MarshalIndent(final.GetAttribute(), "", "  ")
	fmt.Println("\n=== 珠宝属性(结构化)===")
	fmt.Println(string(attr))

	proc, _ := json.MarshalIndent(final.GetProcessList(), "", "  ")
	fmt.Println("\n=== 全流程链路(结构化)===")
	fmt.Println(string(proc))
}

输出:

image

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