• 《golang设计模式》第二部分·结构型模式-05-门面模式Facade)


    1. 概述

    门面(Facade)向客户端提供使用子系统的统一接口,用于简化客户端使用子系统的操作。

    1.1 角色

    • 门面角色(Facade)
      • 客户端可以调用的接口或方法
      • 此角色知晓系统的所有功能和责任
      • 一般情况下,该角色没有实际业务逻辑,只是一个委托类。
    • 子系统角色(Subsystem)
      • 同时有一个或多个子系统
      • 子系统并不知道门面的存在(对于子系统,门面是一个客户端)

    1.2 类图

    Client
    Facade
    SubsystemA
    -Service()
    SubsystemB
    -Service()
    SubsystemC
    -Service()

    2. 代码示例

    2.1 设计

    • 分别创建DeploymentServicePVC三个类
      • 它们的Create()方法给它本身赋值
      • 它们的Get()方法可以查询它本身
    • 创建统一门面Project()
      • 它关联了DeploymentServicePVC三个类
      • 它的Create()方法调用DeploymentServicePVCCreate()方法创建它和所有子系统
      • 它的Get()方法调用DeploymentServicePVCCet()方法查询它和所有子系统
    • 调用
      • 实例化一个门面project
      • 用它的Create()方法统一创建所有子系统
      • 查询结果

    2.2 代码

    package main
    
    import "fmt"
    //创建一个子系统角色
    type Deployment struct {
    	Name   string
    	Kind   string
    	Image  string
    	volume string
    }
    
    func (deployment *Deployment) Create(name string, image string, volume string) {
    	deployment.Name = name
    	deployment.Image = image
    	deployment.Kind = "Deployment"
    	deployment.volume = "pvc:" + volume
    }
    func (deployment *Deployment) Get() {
    	fmt.Printf("%+v\n", deployment)
    }
    //创建第二个子系统角色
    type Service struct {
    	Name     string
    	Kind     string
    	Selector string
    }
    
    func (service *Service) Create(name string, selector string) {
    	service.Name = name
    	service.Kind = "Service"
    	service.Selector = "deployment:" + selector
    }
    func (service *Service) Get() {
    	fmt.Printf("%+v\n", service)
    }
    //创建第三个子系统角色
    type PVC struct {
    	Name string
    	Kind string
    }
    
    func (pvc *PVC) Create(name string) {
    	pvc.Name = name
    	pvc.Kind = "PersistentVolumeClaim"
    }
    func (pvc *PVC) Get() {
    	fmt.Printf("%+v\n", pvc)
    }
    //创建门面,它包含了所有子系统
    type Project struct {
    	Name       string
    	Deployment Deployment
    	Service    Service
    	PVC        PVC
    }
    
    func (project *Project) Create(name string, image string) {
    	project.Name = name
    	project.PVC.Create("pvc_" + name)
    	project.Deployment.Create(name, image, project.PVC.Name)
    	project.Service.Create(name, project.Deployment.Name)
    }
    func (project *Project) Get() {
    	fmt.Println(project.Name)
    	project.Deployment.Get()
    	project.Service.Get()
    	project.PVC.Get()
    }
    
    func main() {
    	project := &Project{}
    	project.Create("nginx", "nginx:1.21")
    	project.Get()
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 输出
    nginx
    &{Name:nginx Kind:Deployment Image:nginx:1.21 volume:pvc:pvc_nginx}
    &{Name:nginx Kind:Service Selector:deployment:nginx}               
    &{Name:pvc_nginx Kind:PersistentVolumeClaim}
    
    • 1
    • 2
    • 3
    • 4

    2.2 类图

    Client
    Project
    +Name:string
    +Create()
    +Get()
    Deployment
    +Name:string
    +Kind:string
    +Image:string
    +volume:string
    +Create()
    +Get()
    Service
    +Name:string
    +Kind:string
    +Selector:string
    +Create()
    +Get()
    PVC
    +Name:string
    +Kind:string
    +Create()
    +Get()

    在这里插入图片描述

  • 相关阅读:
    【论文翻译】Rethinking Network Pruning—under the Pre-train and Fine-tune Paradigm
    UDS诊断入门
    .ttf 字体剔除
    OpenCV(四十二):Harris角点检测
    【17】c++设计模式——>原型模式
    [音视频学习笔记]六、自制音视频播放器Part1 -新版本ffmpeg,Qt +VS2022,都什么年代了还在写传统播放器?
    Leetcode(763)——划分字母区间
    【数据结构--排序】冒泡排序,选择排序,插入排序
    git初学者使用教程(包含Android studio中git使用)
    《MongoDB入门教程》第17篇 文档更新之$mul操作符
  • 原文地址:https://blog.csdn.net/xingzuo_1840/article/details/131796466