go:Decorator Pattern

项目结构:

Go 复制代码
/*
# 版权所有  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
}
Go 复制代码
/*
# 版权所有  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 "基础珠宝"
}
Go 复制代码
/*
# 版权所有  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() + " + 高端礼盒"
}

调用:

Go 复制代码
/*
# 版权所有  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))
}

输出:

相关推荐
坚果派·白晓明10 小时前
【鸿蒙PC三方库移植适配框架解读系列】第八篇:扩展lycium框架使其满足rust三方库适配
c语言·开发语言·华为·rust·harmonyos·鸿蒙
花间相见10 小时前
【PaddleOCR教程01】PP-OCRv5 全面指南:从模型架构到实战部署
开发语言·r语言
小短腿的代码世界11 小时前
Qt 股票订单撮合引擎:高频交易系统的核心心脏
开发语言·数据库·qt·系统架构·交互
UXbot12 小时前
一人独立交付 UI + 前端:AI 驱动 UI 设计工具的五大功能模块深度评测
前端·低代码·ui·设计模式·交互
谙弆悕博士12 小时前
快速学C语言——第16章:预处理
c语言·开发语言·chrome·笔记·创业创新·预处理·业界资讯
yuan1999713 小时前
基于 C# 实现的 Omron HostLink (FINS) 协议 PLC 通讯
开发语言·c#
qq_4228286213 小时前
android图形学之SurfaceControl和Surface的关系 五
android·开发语言·python
如竟没有火炬14 小时前
用队列实现栈
开发语言·数据结构·python·算法·leetcode·深度优先
折哥的程序人生 · 物流技术专研14 小时前
《Java 100 天进阶之路》第17篇:Java常用包装类与自动装箱拆箱深入
java·开发语言·后端·面试
C+++Python15 小时前
C 语言 动态内存分配:malloc /calloc/realloc /free
c语言·开发语言