项目结构:

Go
/*
# 版权所有 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 // 定制单号
}
Go
/*
# 版权所有 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
}
Go
/*
# 版权所有 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,
},
}
}
Go
/*
# 版权所有 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("---------------------------")
}
调用:
Go
/*
# 版权所有 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)
}
输出:
