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

推荐订阅源

D
Docker
Apple Machine Learning Research
Apple Machine Learning Research
宝玉的分享
宝玉的分享
博客园 - 叶小钗
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 司徒正美
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - Franky
爱范儿
爱范儿
罗磊的独立博客
IT之家
IT之家
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
N
Netflix TechBlog - Medium
云风的 BLOG
云风的 BLOG
P
Proofpoint News Feed
U
Unit 42
Engineering at Meta
Engineering at Meta
WordPress大学
WordPress大学
博客园 - 三生石上(FineUI控件)
T
Tailwind CSS Blog
H
Help Net Security
博客园_首页
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
人人都是产品经理
人人都是产品经理

博客园 - ®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: Prototype Pattern
®Geovin Du D · 2026-04-19 · via 博客园 - ®Geovin Du Dream Park™
/*
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Prototype 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/18 21:48
# User      :  geovindu
# Product   : GoLand
# Project   : godesginpattern
# File      : jewelry_model.go
*/
package model

// JewelryModel 珠宝数据模型
type JewelryModel struct {
	// 基础属性(原型复用)
	Material   string  // 材质
	GoldWeight float64 // 金重
	StoneType  string  // 主石
	StoneCarat float64 // 克拉
	Craft      string  // 工艺
	BasePrice  float64 // 基础价

	// 定制属性
	CertNo    string // 证书号
	Engraving string // 刻字
	CustomID  string // 定制单号
}
/*
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Prototype 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/18 21:49
# User      :  geovindu
# Product   : GoLand
# Project   : godesginpattern
# File      : jewelry.go
*/
package prototype

import "godesginpattern/prototype/model"

// Prototype 原型接口
type Prototype interface {
	Clone() Prototype
}

// JewelryPrototype 珠宝原型
type JewelryPrototype struct {
	Data model.JewelryModel // 依赖模型
}

// Clone 克隆方法(深拷贝)
func (j *JewelryPrototype) Clone() Prototype {
	return &JewelryPrototype{
		Data: j.Data, // Go 值拷贝 = 深拷贝
	}
}

// Customize 定制方法
func (j *JewelryPrototype) Customize(certNo, engraving string) {
	j.Data.CertNo = certNo
	j.Data.Engraving = engraving
	j.Data.CustomID = "CUST_" + certNo
}
/*
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Prototype 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/18 21:49
# User      :  geovindu
# Product   : GoLand
# Project   : godesginpattern
# File      : prototype_builder.go
*/
package builder

import (
	"godesginpattern/prototype/model"
	"godesginpattern/prototype/prototype"
)

// NewClassicDiamondRing 经典六爪钻戒原型
func NewClassicDiamondRing() *prototype.JewelryPrototype {
	return &prototype.JewelryPrototype{
		Data: model.JewelryModel{
			Material:   "PT950铂金",
			GoldWeight: 3.2,
			StoneType:  "天然钻石",
			StoneCarat: 0.5,
			Craft:      "六爪镶+高抛光",
			BasePrice:  12999.00,
		},
	}
}

// NewGoldBangle 足金手镯原型
func NewGoldBangle() *prototype.JewelryPrototype {
	return &prototype.JewelryPrototype{
		Data: model.JewelryModel{
			Material:   "足金999",
			GoldWeight: 20.5,
			StoneType:  "无宝石",
			StoneCarat: 0,
			Craft:      "哑光磨砂",
			BasePrice:  8999.00,
		},
	}
}
/*
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Prototype 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/18 21:49
# User      :  geovindu
# Product   : GoLand
# Project   : godesginpattern
# File      : customize_service.go
*/
package service

import (
	"fmt"
	"godesginpattern/prototype/prototype"
)

// CustomizeService 定制服务
type CustomizeService struct{}

// CreateCustomJewelry 克隆原型并生成定制珠宝
func (s *CustomizeService) CreateCustomJewelry(
	proto *prototype.JewelryPrototype,
	certNo, engraving string,
) *prototype.JewelryPrototype {
	// 克隆
	customObj := proto.Clone().(*prototype.JewelryPrototype)
	// 定制
	customObj.Customize(certNo, engraving)
	return customObj
}

// ShowInfo 打印详情
func (s *CustomizeService) ShowInfo(j *prototype.JewelryPrototype) {
	m := j.Data
	fmt.Printf("=== 定制珠宝信息 ===\n")
	fmt.Printf("材质:%s | 金重:%.2fg | 主石:%s(%.2fct)\n", m.Material, m.GoldWeight, m.StoneType, m.StoneCarat)
	fmt.Printf("工艺:%s | 基础价:%.2f\n", m.Craft, m.BasePrice)
	fmt.Printf("证书号:%s | 刻字:%s | 单号:%s\n", m.CertNo, m.Engraving, m.CustomID)
	fmt.Println("---------------------------")
} 

调用:

/*
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Prototype 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/18 21:52
# User      :  geovindu
# Product   : GoLand
# Project   : godesginpattern
# File      : prototypebll.go
真正企业级结构化、严格分层、职责单一、完全可扩展
结构化 + 分层清晰 + 职责单一 + 易扩展
✅ 结构化
严格按照企业级 Go 项目结构分层
✅ 分层明确
model:数据
prototype:克隆
builder:造原型
service:业务服务
cmd:入口
✅ 职责单一
每层只做一件事,互不干扰
✅ 极易扩展
新增珠宝款式:只加 builder
新增属性:只改 model
新增服务:只加 service
新增克隆类型:只加 prototype
✅ 适合生产环境
无冗余、无耦合、可维护、可测试、可文档化
Prototype/
├── go.mod                    // 项目模块
├── cmd/
│   └── main.go               // 系统入口(仅启动,无业务)
├── internal/
│   ├── prototype/            // 原型层:定义可克隆对象(核心)
│   │   └── jewelry.go
│   ├── builder/              // 构建层:创建基础原型(职责单一)
│   │   └── prototype_builder.go
│   ├── service/              // 服务层:批量定制业务逻辑
│   │   └── customize_service.go
│   └── model/                // 模型层:纯数据结构
│       └── jewelry_model.go
└── README.md                 // 架构说明
*/
package bll

import (
	"godesginpattern/prototype/builder"
	"godesginpattern/prototype/service"
)

func PrototypeMain() {
	// 初始化服务
	customService := &service.CustomizeService{}

	// 创建基础原型
	ringProto := builder.NewClassicDiamondRing()
	bangleProto := builder.NewGoldBangle()

	// 批量定制
	ring1 := customService.CreateCustomJewelry(ringProto, "GIA1001", "一生一世·张三")
	ring2 := customService.CreateCustomJewelry(ringProto, "GIA1002", "执子之手·李四")
	bangle1 := customService.CreateCustomJewelry(bangleProto, "NGTC2001", "平安喜乐·王五")

	// 输出
	customService.ShowInfo(ring1)
	customService.ShowInfo(ring2)
	customService.ShowInfo(bangle1)
} 

输出:

image

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