当前位置: 首页 > news >正文

《golang设计模式》第一部分·创建型模式-04-抽象工厂模式(Abstract Factory)

文章目录

  • 1. 概述
    • 1.1 角色
    • 1.2 类图
  • 2. 代码示例
    • 2.1 设计
    • 2.2 代码
    • 2.3 类图

1. 概述

1.1 角色

  • AbstractFactory(抽象工厂):它声明了一组用于创建产品的方法,每一个方法对应一种产品。
  • ConcreteFactory(具体工厂):它实现了在抽象工厂中声明的创建产品的方法,生成一组具体产品,这些产品构成了一个产品族,每一个产品都位于某个产品等级结构中。
  • AbstractProduct(抽象产品):它为每种产品声明接口,在抽象产品中声明了产品所具有的业务方法。
  • ConcreteProduct(具体产品):它定义具体工厂生产的具体产品对象,实现抽象产品接口中声明的业务方法。

1.2 类图

«interface»
ProductA
ProductA1
ProductA2
«interface»
ProductB
ProductB1
ProductB2
Client
«interface»
AbstractFactory
+CreateProductA() : ProductA
+CreateProductB() : ProductB
ConcreteFactory1
+CreateProductA() : ProductA
+CreateProductB() : ProductB
ConcreteFactory2
+CreateProductA() : ProductA
+CreateProductB() : ProductB

2. 代码示例

2.1 设计

  • 定义抽象产品A
    • 定义具体产品A-1
      • 它有NameWeight两个成员
      • 它的SetName()SetWeight()两个方法分别负责设置以上两个成员
      • 它的Get()方法获取它本身的信息
    • 定义具体产品A-2
      • 它有NameWeight两个成员
      • 它的SetName()SetWeight()两个方法分别负责设置以上两个成员
      • 它的Get()方法获取它本身的信息
  • 定义抽象产品B
    • 定义具体产品B-1
      • 它有NameHeight两个成员
      • 它的SetName()SetHeight()两个方法分别负责设置以上两个成员
      • 它的Get()方法获取它本身的信息
    • 定义具体产品B-2
      • 它有NameHeight两个成员
      • 它的SetName()SetHeight()两个方法分别负责设置以上两个成员
      • 它的Get()方法获取它本身的信息
  • 定义抽象工厂
    • 定义具体工厂1
      • 它的方法SetProductA()设置具体产品A-1
      • 它的方法SetProductB()设置具体产品B-1
    • 定义具体工厂2
      • 它的方法SetProductA()设置具体产品A-2
      • 它的方法SetProductB()设置具体产品B-2
  • 定义一个函数CreateFactory(),用来创建具体工厂
  • 调用
    • CreateFactory()函数实例化 具体工厂1——factory1
    • factory1实例化产品A-1产品B-1,并查看结果
    • CreateFactory()函数实例化 具体工厂2——factory2
    • factory2实例化产品A-2产品B-2,并查看结果

2.2 代码

package mainimport "fmt"// 定义抽象产品A
type AbstractProductA interface {SetName(name string)SetWeight(weight int64)Get()
}// 定义具体产品A-1
type ConcreteProductA1 struct {Name   stringWeight int64
}func (c *ConcreteProductA1) SetName(name string) {c.Name = name
}func (c *ConcreteProductA1) SetWeight(weight int64) {c.Weight = weight
}func (c *ConcreteProductA1) Get() {fmt.Printf("%v\n", c)
}// 定义具体产品A-2
type ConcreteProductA2 struct {Name   stringWeight int64
}func (c *ConcreteProductA2) SetName(name string) {c.Name = name
}func (c *ConcreteProductA2) SetWeight(weight int64) {c.Weight = weight
}
func (c *ConcreteProductA2) Get() {fmt.Printf("%v\n", c)
}// 定义抽象产品B
type AbstractProductB interface {SetName(name string)SetHeight(height int64)Get()
}// 定义具体抽象产品B-1
type ConcreteProductB1 struct {Name   stringHeight int64
}func (c *ConcreteProductB1) SetName(name string) {c.Name = name
}func (c *ConcreteProductB1) SetHeight(height int64) {c.Height = height
}
func (c *ConcreteProductB1) Get() {fmt.Printf("%v\n", c)
}// 定义具体产品B-2
type ConcreteProductB2 struct {Name   stringHeight int64
}func (c *ConcreteProductB2) SetName(name string) {c.Name = name
}func (c *ConcreteProductB2) SetHeight(height int64) {c.Height = height
}
func (c *ConcreteProductB2) Get() {fmt.Printf("%v\n", c)
}// Factory部分// 定义抽象工厂
type Factory interface {SetProductA(name string, weight int64) AbstractProductASetProductB(name string, height int64) AbstractProductB
}// 定义具体工厂1
type Factory1 struct {
}func (f *Factory1) SetProductA(name string, weight int64) AbstractProductA {a := &ConcreteProductA1{}a.SetName(name)a.SetWeight(weight)return a
}func (f *Factory1) SetProductB(name string, height int64) AbstractProductB {a := &ConcreteProductB1{}a.SetName(name)a.SetHeight(height)return a
}// 定义具体工厂2
type Factory2 struct {
}func (f *Factory2) SetProductA(name string, weight int64) AbstractProductA {a := &ConcreteProductA2{}a.SetName(name)a.SetWeight(weight)return a
}func (f *Factory2) SetProductB(name string, height int64) AbstractProductB {a := &ConcreteProductB2{}a.SetName(name)a.SetHeight(height)return a
}// 写一个创建工厂的函数
func CreateFactory(pType int64) Factory {switch pType {case 1:return &Factory1{}case 2:return &Factory2{}}return nil
}func main() {factory1 := CreateFactory(1)factory1.SetProductA("A1", 3).Get()factory1.SetProductB("B1", 3).Get()factory2 := CreateFactory(2)factory2.SetProductA("A2", 4).Get()factory2.SetProductB("B2", 4).Get()
}
  • 输出
&{A1 3}
&{B1 3}
&{A2 4}
&{B2 4}

2.3 类图

«interface»
ProductA
+SetName(name string)
+SetWeight(weight int64)
+Get()
ProductA1
-Name string
-Weight int64
+SetName(name string)
+SetWeight(weight int64)
+Get()
ProductA2
-String Name
-Int64 Weight
+SetName(name string)
+SetWeight(weight int64)
+Get()
«interface»
ProductB
+SetName(name string)
+SetHeight(height int64)
+Get()
ProductB1
-String Name
-Int64 Height
+SetName(name string)
+SetHeight(height int64)
+Get()
ProductB2
-String Name
-Int64 Height
+SetName(name string)
+SetHeight(height int64)
+Get()
Client
«interface»
AbstractFactory
+SetProductA(name string, weight int64) : AbstractProductA
+SetProductB(name string, height int64) : AbstractProductB
ConcreteFactory1
+SetProductA(name string, weight int64) : AbstractProductA
+SetProductB(name string, height int64) : AbstractProductB
ConcreteFactory2
+SetProductA(name string, weight int64) : AbstractProductA
+SetProductB(name string, height int64) : AbstractProductB

在这里插入图片描述

http://www.lryc.cn/news/106727.html

相关文章:

  • 改进粒子群算法优化BP神经网络---回归+分类两种案例
  • VSCode和QT联合开发
  • YOLO5-1 使用YOLO5检测 水面漂浮物记录
  • MongoDB教程-7
  • Redisson提供优秀的并发控制机制
  • Linux: 设置qmake的Qt版本
  • 使用LLM插件从命令行访问Llama 2
  • gateway过滤器没生效,特殊原因
  • 长相思追剧小游戏
  • leetcode做题笔记51
  • Windows同时安装两个版本的JDK并随时切换,以JDK6和JDK8为例,并解决相关存在的问题(亲测有效)
  • 【ChatGPT辅助学Rust | 基础系列 | Cargo工具】Cargo介绍及使用
  • 全面了解CPU Profiler:解读CPU性能分析工具的核心功能与用法
  • rust format!如何转义{},输出{}?
  • 真人AI写真的制作方法-文生图换脸
  • vscode如何包含第三方库
  • 【Docker】Docker安装Consul
  • 《吐血整理》进阶系列教程-拿捏Fiddler抓包教程(20)-Fiddler精选插件扩展安装让你的Fiddler开挂到你怀疑人生
  • 计算机top命令
  • DevExpress WPF Tree List组件,让数据可视化程度更高!(二)
  • lc1074.元素和为目标值的子矩阵数量
  • elementUi el-radio神奇的:label与label不能设置默认值
  • git仓库清理
  • 从0到1开发go-tcp框架【3-读写协程分离、引入消息队列、进入连接管理器、引入连接属性】【基础篇完结】
  • python-爬虫作业
  • vue3+ts+pinia整合websocket
  • 【微信小程序】保存多张图片到本地相册
  • Python Numpy入门基础(二)数组操作
  • 【LeetCode每日一题】——1572.矩阵对角线元素的和
  • 牛客网Verilog刷题——VL55