项目结构:

Go
/*
# 版权所有 2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Proxy 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/21 20:26
# User : geovindu
# Product : GoLand
# Project : godesginpattern
# File : errors.go
*/
package common
import "errors"
var ErrAccessDenied = errors.New("权限不足:非VIP顾客无法查看高端珠宝")
Go
/*
# 版权所有 2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Proxy 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/21 20:27
# User : geovindu
# Product : GoLand
# Project : godesginpattern
# File : jewelry.go
*/
package api
// LuxuryJewelry 高端珠宝统一接口
type LuxuryJewelry interface {
Display() error
GetName() string
}
Go
/*
# 版权所有 2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Proxy 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/21 20:27
# User : geovindu
# Product : GoLand
# Project : godesginpattern
# File : jewelry.go
*/
package domain
import "fmt"
// DiamondNecklace 钻石项链
type DiamondNecklace struct {
name string
price float64
quality string
crafts string
}
func NewDiamondNecklace() *DiamondNecklace {
return &DiamondNecklace{
name: "海洋之心典藏钻石项链",
price: 12800000,
quality: "VVS1 无瑕级",
crafts: "法国手工微镶",
}
}
func (d *DiamondNecklace) Display() error {
fmt.Printf("\n【核心珠宝展示】\n")
fmt.Printf("品名:%s\n", d.name)
fmt.Printf("价值:%.0f 元\n", d.price)
fmt.Printf("品质:%s\n", d.quality)
fmt.Printf("工艺:%s\n", d.crafts)
return nil
}
func (d *DiamondNecklace) GetName() string {
return d.name
}
// RubyRing 红宝石戒指
type RubyRing struct {
name string
price float64
color string
}
func NewRubyRing() *RubyRing {
return &RubyRing{
name: "缅甸鸽血红宝石戒指",
price: 6800000,
color: "鸽血红",
}
}
func (r *RubyRing) Display() error {
fmt.Printf("\n【核心珠宝展示】\n")
fmt.Printf("品名:%s\n", r.name)
fmt.Printf("价值:%.0f 元\n", r.price)
fmt.Printf("色级:%s\n", r.color)
return nil
}
func (r *RubyRing) GetName() string {
return r.name
}
/*
# 版权所有 2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Proxy 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/21 20:28
# User : geovindu
# Product : GoLand
# Project : godesginpattern
# File : customer.go
*/
package domain
// Customer 顾客实体
type Customer struct {
ID string
Level string // VIP / NORMAL
RealName string
}
Go
/*
# 版权所有 2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Proxy 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/21 20:27
# User : geovindu
# Product : GoLand
# Project : godesginpattern
# File : jewelry_proxy.go
*/
package proxy
import (
"fmt"
"godesginpattern/proxy/api"
"godesginpattern/proxy/common"
"godesginpattern/proxy/domain"
"time"
)
// AccessLog 访问日志
type AccessLog struct {
CustomerID string
JewelryName string
VisitTime time.Time
Success bool
Message string
}
// JewelryProxy 珠宝代理
type JewelryProxy struct {
realJewelry api.LuxuryJewelry
customer *domain.Customer
}
func NewJewelryProxy(realJewelry api.LuxuryJewelry, customer *domain.Customer) *JewelryProxy {
return &JewelryProxy{
realJewelry: realJewelry,
customer: customer,
}
}
// 权限校验
func (p *JewelryProxy) checkPermission() error {
if p.customer.Level != "VIP" {
return common.ErrAccessDenied
}
fmt.Printf("[权限校验] ✅ 顾客 %s(%s) 权限通过\n", p.customer.RealName, p.customer.ID)
return nil
}
// 记录日志
func (p *JewelryProxy) recordLog(success bool, msg string) {
log := AccessLog{
CustomerID: p.customer.ID,
JewelryName: p.realJewelry.GetName(),
VisitTime: time.Now(),
Success: success,
Message: msg,
}
fmt.Printf("[访问日志] 📝 记录:%v\n", log)
}
// 安全保护
func (p *JewelryProxy) securityProtect() {
fmt.Println("[安全保护] 🔒 珠宝已开启红外防盗 + 全程监控")
}
// Display 实现统一接口
func (p *JewelryProxy) Display() error {
if err := p.checkPermission(); err != nil {
p.recordLog(false, err.Error())
return err
}
p.securityProtect()
err := p.realJewelry.Display()
if err != nil {
p.recordLog(false, "展示失败:"+err.Error())
return err
}
p.recordLog(true, "展示成功")
fmt.Println("\n---------------------------------------")
return nil
}
调用:
Go
/*
# 版权所有 2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Proxy 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/21 20:31
# User : geovindu
# Product : GoLand
# Project : godesginpattern
# File : proxybll.go
proxy/
├── go.mod
├── main.go # 入口:组装依赖、启动
├── api/
│ └── jewelry.go # 接口层:统一契约定义
├── domain/
│ ├── jewelry.go # 领域:真实珠宝对象
│ └── customer.go # 领域:顾客模型
├── proxy/
│ └── jewelry_proxy.go # 代理层:访问控制、日志、安全
└── common/
└── errors.go # 公共错误定义
*/
package bll
import (
"godesginpattern/proxy/domain"
"godesginpattern/proxy/proxy"
)
func ProxyMain() {
println("========== 高端珠宝展厅(企业级代理模式) ==========\n")
// 顾客
vip := &domain.Customer{ID: "VIP_001", Level: "VIP", RealName: "张先生"}
normal := &domain.Customer{ID: "CUS_002", Level: "NORMAL", RealName: "李先生"}
// 真实珠宝
necklace := domain.NewDiamondNecklace()
ring := domain.NewRubyRing()
// 代理
p1 := proxy.NewJewelryProxy(necklace, vip)
p2 := proxy.NewJewelryProxy(ring, vip)
p3 := proxy.NewJewelryProxy(necklace, normal)
// 调用
_ = p1.Display()
_ = p2.Display()
_ = p3.Display()
}
输出:
