项目结构:

Go
/*
# 版权所有 2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Factory Method Pattern Go 工厂方法模式
# 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/12 12:16
# User : geovindu
# Product : GoLand
# Project : sudydemo
# File : jewelry.go
*/
package factorymethod
// Jewelry 珠宝产品接口
type Jewelry interface {
// 核心属性
GetMaterial() string // 获取材质
GetWeight() float64 // 获取克重
GetGem() string // 获取宝石
// 统一生产流程
Design() // 设计
Make() // 制作
Check() // 质检
Price() // 定价
}
/*
# 版权所有 2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Factory Method Pattern Go 工厂方法模式
# 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/12 12:16
# User : geovindu
# Product : GoLand
# Project : sudydemo
# File : factory.go
*/
package factorymethod
// JewelryFactory 珠宝工厂接口
type JewelryFactory interface {
CreateJewelry(material string, weight float64, gem string) Jewelry
}
/*
# 版权所有 2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Factory Method Pattern Go 工厂方法模式
# 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/12 12:17
# User : geovindu
# Product : GoLand
# Project : sudydemo
# File : ring.go
*/
package factorymethod
import "fmt"
// Ring 戒指(具体产品)
type Ring struct {
material string // 材质
weight float64 // 克重
gem string // 宝石
}
// 实现 Jewelry 接口的核心属性
func (r *Ring) GetMaterial() string { return r.material }
func (r *Ring) GetWeight() float64 { return r.weight }
func (r *Ring) GetGem() string { return r.gem }
// 实现 Jewelry 接口的生产流程
func (r *Ring) Design() {
fmt.Printf("【戒指】设计:%s材质,镶嵌%s\n", r.material, r.gem)
}
// 生产
func (r *Ring) Make() {
fmt.Printf("【戒指】制作:%.2f克,打造戒托\n", r.weight)
}
// 选择
// @pagram
func (r *Ring) Check() {
fmt.Println("【戒指】质检:尺寸、镶嵌、抛光合格")
}
// 价格
func (r *Ring) Price() {
price := r.weight*500.0 + 2000.0 // 简化定价公式
fmt.Printf("【戒指】定价:%.2f 元\n\n", price)
}
// RingFactory 戒指工厂(具体工厂)
type RingFactory struct{}
// 实现工厂接口,只生产戒指
// @Param material
// @Param weight
// @Param gem
// @Return ${return_types}
func (r *RingFactory) CreateJewelry(material string, weight float64, gem string) Jewelry {
return &Ring{
material: material,
weight: weight,
gem: gem,
}
}
/*
# 版权所有 2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Factory Method Pattern Go 工厂方法模式
# 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/12 12:18
# User : geovindu
# Product : GoLand
# Project : sudydemo
# File : necklace.go
*/
package factorymethod
import "fmt"
// Necklace 项链(具体产品)
type Necklace struct {
material string
weight float64
gem string
}
// 核心属性实现
func (n *Necklace) GetMaterial() string { return n.material }
func (n *Necklace) GetWeight() float64 { return n.weight }
func (n *Necklace) GetGem() string { return n.gem }
// 生产流程实现
func (n *Necklace) Design() {
fmt.Printf("【项链】设计:%s材质,链条镶嵌%s\n", n.material, n.gem)
}
func (n *Necklace) Make() {
fmt.Printf("【项链】制作:%.2f克,锻造链条+吊坠\n", n.weight)
}
func (n *Necklace) Check() {
fmt.Println("【项链】质检:链条韧性、宝石牢固度合格")
}
func (n *Necklace) Price() {
price := n.weight*480.0 + 1800.0
fmt.Printf("【项链】定价:%.2f 元\n\n", price)
}
// NecklaceFactory 项链工厂
type NecklaceFactory struct{}
// 只生产项链
// @Param material
// @Param weight
// @Param gem
// @Return Necklace
func (n *NecklaceFactory) CreateJewelry(material string, weight float64, gem string) Jewelry {
return &Necklace{
material: material,
weight: weight,
gem: gem,
}
}
/*
# 版权所有 2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Factory Method Pattern Go 工厂方法模式
# 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/12 12:18
# User : geovindu
# Product : GoLand
# Project : sudydemo
# File : bracelet.go
*/
package factorymethod
import "fmt"
// Bracelet 手镯(具体产品)
type Bracelet struct {
material string
weight float64
gem string
}
// 核心属性实现
func (b *Bracelet) GetMaterial() string { return b.material }
func (b *Bracelet) GetWeight() float64 { return b.weight }
func (b *Bracelet) GetGem() string { return b.gem }
// 生产流程实现
// @Title 生产流程实现
// @Description ${user}
// @Author geovindu ${date} ${time}
// @Param name
// @Return string
func (b *Bracelet) Design() {
fmt.Printf("【手镯】设计:%s材质,镯身镶嵌%s\n", b.material, b.gem)
}
// @Title 生产流程实现
// @Description ${user}
// @Author geovindu ${date} ${time}
// @Param name
// @Return string
func (b *Bracelet) Make() {
fmt.Printf("【手镯】制作:%.2f克,弯曲成型+打磨\n", b.weight)
}
// @Title 生产流程实现
// @Description ${user}
// @Author geovindu ${date} ${time}
// @Param name
// @Return string
func (b *Bracelet) Check() {
fmt.Println("【手镯】质检:弧度、舒适度、镶嵌合格")
}
// @Title 生产流程实现
// @Description ${user}
// @Author geovindu ${date} ${time}
// @Param name
// @Return string
func (b *Bracelet) Price() {
price := b.weight*450.0 + 1500.0
fmt.Printf("【手镯】定价:%.2f 元\n\n", price)
}
// BraceletFactory 手镯工厂
type BraceletFactory struct{}
// 只生产手镯
// @Title 生产流程实现 手镯
// @Description ${user}
// @Author geovindu ${date} ${time}
// @Param name
// @Return string
func (b *BraceletFactory) CreateJewelry(material string, weight float64, gem string) Jewelry {
return &Bracelet{
material: material,
weight: weight,
gem: gem,
}
}
调用:
Go
/*
# 版权所有 2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:
# 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/12 18:56
# User : geovindu
# Product : GoLand
# Project : sudydemo
# File : factorymethodbll.go
*/
package bll
import (
"fmt"
"sudydemo/factorymethod"
)
// 统一执行珠宝生产流程
func produceJewelry(f factorymethod.JewelryFactory, material string, weight float64, gem string) {
jewelry := f.CreateJewelry(material, weight, gem)
jewelry.Design()
jewelry.Make()
jewelry.Check()
jewelry.Price()
}
// 显示示例
func FactoryMethodMain() {
fmt.Println("===Factory Method Pattern工厂方法模式===")
// 1. 戒指工厂生产钻戒
ringFactory := &factorymethod.RingFactory{}
produceJewelry(ringFactory, "铂金", 5.8, "钻石")
// 2. 项链工厂生产宝石项链
necklaceFactory := &factorymethod.NecklaceFactory{}
produceJewelry(necklaceFactory, "黄金", 12.3, "红宝石")
// 3. 手镯工厂生产翡翠手镯
braceletFactory := &factorymethod.BraceletFactory{}
produceJewelry(braceletFactory, "足银", 20.5, "翡翠")
}
main():
Go
FactoryMethodMain()
输出:
