go:Decorator Pattern

项目结构:

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

 

posted @ 2026-04-20 21:52  ®Geovin Du Dream Park™  阅读(7)  评论(0)    收藏  举报