go: Facade Pattern

项目结构:

Go 复制代码
/*
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Facade 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:24
# User      :  geovindu
# Product   : GoLand
# Project   : godesginpattern
# File      : ring.go
*/
package domain
 
// RingOrder 钻戒定制入参
type RingOrder struct {
    Carat    float64
    Clarity  string
    Material string
}
Go 复制代码
/*
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Facade 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:24
# User      :  geovindu
# Product   : GoLand
# Project   : godesginpattern
# File      : ring.go
*/
package service
 
import (
    "fmt"
    "godesginpattern/facade/domain"
)
 
// IRing 戒托制作接口
type IRing interface {
    Make(domain.RingOrder)
}
 
type ringImpl struct{}
 
func NewRing() IRing {
    return &ringImpl{}
}
 
func (r *ringImpl) Make(ord domain.RingOrder) {
    fmt.Printf("✅ 戒托制作:制作【%s】材质的戒托成功\n", ord.Material)
}
 
 
 
/*
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Facade 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:24
# User      :  geovindu
# Product   : GoLand
# Project   : godesginpattern
# File      : inlay.go
*/
package service
 
import "fmt"
 
// IInlay 镶嵌接口
type IInlay interface {
    DoInlay()
}
 
type inlayImpl struct{}
 
func NewInlay() IInlay {
    return &inlayImpl{}
}
 
func (i *inlayImpl) DoInlay() {
    fmt.Println("✅ 珠宝镶嵌:将钻石镶嵌到戒托上成功")
}
 
 
/*
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Facade 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:24
# User      :  geovindu
# Product   : GoLand
# Project   : godesginpattern
# File      : diamond.go
*/
package service
 
import (
    "fmt"
    "godesginpattern/facade/domain"
)
 
// IDiamond 钻石采购接口
type IDiamond interface {
    Buy(domain.RingOrder)
}
 
type diamondImpl struct{}
 
func NewDiamond() IDiamond {
    return &diamondImpl{}
}
 
func (d *diamondImpl) Buy(ord domain.RingOrder) {
    fmt.Printf("✅ 钻石采购:采购【%.2f克拉,净度%s】的钻石成功\n", ord.Carat, ord.Clarity)
}
 
 
/*
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Facade 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:25
# User      :  geovindu
# Product   : GoLand
# Project   : godesginpattern
# File      : cert.go
*/
package service
 
import "fmt"
 
// ICert 证书办理接口
type ICert interface {
    Apply()
}
 
type certImpl struct{}
 
func NewCert() ICert {
    return &certImpl{}
}
 
func (c *certImpl) Apply() {
    fmt.Println("✅ 证书办理:成品钻戒鉴定证书办理成功")
}
Go 复制代码
/*
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Facade 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:25
# User      :  geovindu
# Product   : GoLand
# Project   : godesginpattern
# File      : ring_facade.go
*/
package facade
 
import (
    "fmt"
    "godesginpattern/facade/domain"
    "godesginpattern/facade/service"
)
 
// IRingFacade 外观统一接口
type IRingFacade interface {
    Custom(ord domain.RingOrder)
}
 
type ringFacade struct {
    dia   service.IDiamond
    ring  service.IRing
    inlay service.IInlay
    cert  service.ICert
}
 
// NewRingFacade 依赖注入,完全解耦
func NewRingFacade(
    dia service.IDiamond,
    ring service.IRing,
    inlay service.IInlay,
    cert service.ICert,
) IRingFacade {
    return &ringFacade{
        dia:   dia,
        ring:  ring,
        inlay: inlay,
        cert:  cert,
    }
}
 
func (f *ringFacade) Custom(ord domain.RingOrder) {
    fmt.Println("======== 开始定制钻戒 ========")
    f.dia.Buy(ord)
    f.ring.Make(ord)
    f.inlay.DoInlay()
    f.cert.Apply()
    fmt.Println("======== 钻戒定制完成,可取货 ========\n")
}

调用:

Go 复制代码
/*
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Facade 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:28
# User      :  geovindu
# Product   : GoLand
# Project   : godesginpattern
# File      : facadebll.go
facade/
├── domain
│   └── ring.go        // 领域模型
├── service
│   ├── diamond.go     // 钻石采购子系统
│   ├── ring.go        // 戒托制作子系统
│   ├── inlay.go       // 镶嵌子系统
│   └── cert.go        // 证书子系统
├── facade
│   └── ring_facade.go // 外观门面层
├── go.mod
└── main.go
*/
package bll
 
import (
    "godesginpattern/facade/domain"
    "godesginpattern/facade/facade"
    "godesginpattern/facade/service"
)
 
// 示例
func FacadeMain() {
    // 依赖注入初始化子系统
    diaSvc := service.NewDiamond()
    ringSvc := service.NewRing()
    inlaySvc := service.NewInlay()
    certSvc := service.NewCert()
 
    // 外观门面
    shop := facade.NewRingFacade(diaSvc, ringSvc, inlaySvc, certSvc)
 
    // 定制单1
    shop.Custom(domain.RingOrder{
        Carat:    1.0,
        Clarity:  "VVS",
        Material: "铂金",
    })
 
    // 定制单2
    shop.Custom(domain.RingOrder{
        Carat:    0.5,
        Clarity:  "VS",
        Material: "18K金",
    })
}

输出:

相关推荐
小众AI2 小时前
Go 多账户 WebDAV 服务实现
golang
念何架构之路3 小时前
图解defer
开发语言·后端·golang
旷世奇才李先生3 小时前
React 18\+TypeScript实战: hooks封装与组件设计模式
react.js·设计模式·typescript
白夜11173 小时前
C++设计模式(高内聚,低耦合)
c++·设计模式
ximu_polaris3 小时前
设计模式(C++)-结构型模式-桥接模式
c++·设计模式·桥接模式
楼田莉子3 小时前
仿muduo库的高并发服务器——正则表达式与any类介绍及其简单模拟实现
linux·服务器·c++·学习·设计模式
workflower3 小时前
机器人应用-室外区域巡逻
人工智能·设计模式·机器人·软件工程·软件构建
我喜欢山,也喜欢海4 小时前
Java和go在并发上的表现为什么不一样
java·python·golang
geovindu5 小时前
go: Flyweight Pattern
开发语言·设计模式·golang·享元模式