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

Go语言设计模式(三)抽象工厂模式

抽象工厂模式与工厂模式类似,被认为是工厂方法模式的另一层抽象.抽象工厂模式围绕创建其他工厂的超级工厂工作.

1.角色:

1.1抽象产品:

构成产品系列的一组不同但相关的产品的声明接口.

1.2具体产品:

实现抽象产品接口的类,主要用于定义产品对象,由相应的具体工厂创建.

1.3抽象工厂:

创建抽象产品对象的操作接口.

1.4具体工厂:

实现抽象工厂接口的类.用于创建产品对象.每个具体工厂都会生产相应的具体产品.

1.5客户端:

通过抽象接口调用抽象工厂对象和抽象产品对象,客户端能与所有具体工厂或具体产品交互.

2.抽象工厂使用场景:

2.1出于对代码未来扩展性考虑.不希望代码基于具体产品进行构建,可以使用抽象工厂模式.

2.2某个类具有一组抽象方法,并且这个类功能不够明确,可以考虑抽象工厂模式.

2.3如果一个类需要与多种类型的产品交互,可以考虑将工厂方法抽取到具备完整功能的抽象工厂接口中.

3.实现方式:

3.1抽象产品接口:
package itboStudyimport "fmt"// 抽象产品接口
type AbstractProduct interface {GetName() 
}// 具体产品类
type Computer struct {
}//具体产品类中的方法
func (c *Computer) GetName() {fmt.Println("具体产品Computer")
}
3.2抽象工厂接口:
//抽象工厂接口
type AbstractFactory interface {CreateProduct() AbstractProduct
}
3.3具体工厂类及方法:
// 具体工厂类
type ComputerProductFactory struct{}// 初始化具体工厂对象.
func NewComputerProductFactory() ComputerProductFactory {return ComputerProductFactory{}
}// 使用具体工厂对象创建具体产品
func (com *ComputerProductFactory) CreateProduct() Computer {return Computer{}
}
3.4客户端:
func main() {factory := itboStudy.NewComputerProductFactory()product := factory.CreateProduct()product.GetName()
}

4.实战:

4.1抽象接口:
// 电子产品工厂
type InterfaceElectronicFactory interface {MakeComputer() InterfaceComputer
}// 获取电子产品工厂对象
func GetElectronicFactory(brand string) (*LenovoFactory, error) {if brand == "电脑" {return &LenovoFactory{}, nil}return nil, fmt.Errorf("%s", "error brand type")
}
4.2具体工厂类:
// 定义具体工厂类
type LenovoFactory struct{}// 生成联想电脑
func (com *LenovoFactory) MakeComputer() *LenovoComputer {return &LenovoComputer{Computer: Computer{color: "black",size:  14,},}
}
4.3抽象产品接口:
type InterfaceComputer interface {SetColor(color string)SetSize(size int)Getcolor() stringGetsize() int
}type Computer struct {color stringsize  int
}func (com *Computer) SetColor(color string) {com.color = color
}func (com *Computer) SetSize(size int) {com.size = size
}func (com *Computer) GetSize() int {return com.size
}func (com *Computer) GetColor() string {return com.color
}
4.4具体产品类:
// 定义具体产品
type LenovoComputer struct {Computer
}
4.5客户端:
func main() {factory, _ := itboStudy.GetElectronicFactory("电脑")computer := factory.MakeComputer()fmt.Printf("computer:%#v\n", computer.GetColor())
}

5优点:

客户端不知道创建什么类型对象时.

抽象工厂实现了具体的隔离.

抽象工厂可以轻松改变产品系列.

保证产品一致性.

6.缺点:

抽象工厂难以扩展新型产品.如果要支持新型产品需要扩展工厂接口.

可以迟到,但是不会缺席.

http://www.xdnf.cn/news/1476217.html

相关文章:

  • ModelScope概述与实战
  • GitHub 热榜项目 - 日榜(2025-09-06)
  • PowerBI TopN Others
  • tp报错解决
  • 【Gigascience】时空转录组测序探索小鼠心脏发育的细胞与分子基础
  • 留数法分解有理分式
  • Rust在医疗系统中的应用:安全、性能与合规性实践(上)
  • 3.进程调度:常见算法
  • leetcode30.串联所有单词的子串
  • [数据结构] LinkedList
  • c++之基础B(x转10进制,含十六进制)(第四课)
  • 7.网络虚拟化
  • 【开题答辩全过程】以 基于Hadoop电商数据的可视化分析为例,包含答辩的问题和答案
  • Lua和C#比较
  • 分布式go项目-搭建监控和追踪方案补充-ELK日志收集
  • OpenHarmony之有源NFC-connected_nfc_tag模块详解
  • LangChain实战(十八):构建ReAct模式的网页内容摘要与分析Agent
  • 同一台nginx中配置多个前端项目的三种方式
  • 贪心算法在脑机接口解码问题中的应用
  • qiankun 微前端接入实战
  • 在线教育系统源码选型指南:功能、性能与扩展性的全面对比
  • import type在模块引入中的作用
  • 从“能说话”到“会做事”:AI工具如何重塑普通人的工作与生活?
  • 语义切片技术深度解析:重新定义RAG时代的文本处理范式
  • 分布式通信平台测试报告
  • 【Neovim】Vi、Vim、Neovim 与 LazyVim:发展史
  • 【开题答辩全过程】以 “爱心”家政管理系统为例,包含答辩的问题和答案
  • Linux/UNIX系统编程手册笔记:共享库、进程间通信、管道和FIFO、内存映射以及虚拟内存操作
  • 宝塔PostgreSQL安装pgvecto插件contrib包实现向量存储
  • 2025年渗透测试面试题总结-54(题目+回答)