Golang学习条记_28——工厂方法模式

农民  论坛元老 | 2025-1-21 16:10:43 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 1013|帖子 1013|积分 3039

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
Golang学习条记_25——协程
Golang学习条记_26——通道
Golang学习条记_27——单例模式


  

工厂方法模式

1. 介绍

工厂方法模式(Factory Method)是一种创建型设计模式,它提供了一种创建对象的接口,但由子类决定要实例化的类是哪一个。工厂方法模式让类的实例化推迟到子类中进行
2. 优点


  • 解耦:将对象的创建与使用分离,客户端不必要知道具体的类名。
  • 扩展性:增加新的产物类时,只需添加相应的工厂类即可,符合开闭原则。
  • 单一职责原则:将创建对象的代码封装在工厂中,职责单一。
3. 类图


  • Product(产物接口):界说了产物的接口,所有具体产物都要实现这个接口。
  • ConcreteProduct(具体产物):实现了Product接口的具体类。
  • Creator(工厂接口):声明了工厂方法,用于返回一个Product对象。
  • ConcreteCreator(具体工厂):实现了工厂接口,返回具体产物的实例。
  1.     +-----------------+
  2.     |     Product     |<----------------+
  3.     +-----------------+                 |
  4.     | + Use()         |                 |
  5.     +-----------------+                 |
  6.             ^                           |
  7.             |                           |
  8.     +-----------------+         +-----------------+
  9.     | ConcreteProductA|         | ConcreteProductB|
  10.     +-----------------+         +-----------------+
  11.     | + Use()         |         | + Use()         |
  12.     +-----------------+         +-----------------+
  13.     +-----------------+
  14.     |     Creator     |<----------------+
  15.     +-----------------+                 |
  16.     | + CreateProduct()|                |
  17.     +-----------------+                 |
  18.             ^                           |
  19.             |                           |
  20.     +-----------------+         +-----------------+
  21.     | ConcreteCreatorA|         | ConcreteCreatorB|
  22.     +-----------------+         +-----------------+
  23.     | + CreateProduct()|        | + CreateProduct()|
  24.     +-----------------+         +-----------------+
复制代码
4. 实现

  1. // Product 是产品接口
  2. type Product interface {
  3.         Use() string
  4. }
  5. // ConcreteProductA 是具体产品A
  6. type ConcreteProductA struct{}
  7. func (p *ConcreteProductA) Use() string {
  8.         return "使用产品A"
  9. }
  10. // ConcreteProductB 是具体产品B
  11. type ConcreteProductB struct{}
  12. func (p *ConcreteProductB) Use() string {
  13.         return "使用产品B"
  14. }
  15. // Factory 是工厂接口
  16. type Factory interface {
  17.         CreateProduct() Product
  18. }
  19. // ConcreteFactoryA 是具体工厂A
  20. type ConcreteFactoryA struct{}
  21. func (f *ConcreteFactoryA) CreateProduct() Product {
  22.         return &ConcreteProductA{}
  23. }
  24. // ConcreteFactoryB 是具体工厂B
  25. type ConcreteFactoryB struct{}
  26. func (f *ConcreteFactoryB) CreateProduct() Product {
  27.         return &ConcreteProductB{}
  28. }
复制代码
源码

  1. // Product 是产品接口
  2. type Product interface {
  3.         Use() string
  4. }
  5. // ConcreteProductA 是具体产品A
  6. type ConcreteProductA struct{}
  7. func (p *ConcreteProductA) Use() string {
  8.         return "使用产品A"
  9. }
  10. // ConcreteProductB 是具体产品B
  11. type ConcreteProductB struct{}
  12. func (p *ConcreteProductB) Use() string {
  13.         return "使用产品B"
  14. }
  15. // Factory 是工厂接口
  16. type Factory interface {
  17.         CreateProduct() Product
  18. }
  19. // ConcreteFactoryA 是具体工厂A
  20. type ConcreteFactoryA struct{}
  21. func (f *ConcreteFactoryA) CreateProduct() Product {
  22.         return &ConcreteProductA{}
  23. }
  24. // ConcreteFactoryB 是具体工厂B
  25. type ConcreteFactoryB struct{}
  26. func (f *ConcreteFactoryB) CreateProduct() Product {
  27.         return &ConcreteProductB{}
  28. }
  29. func test() {        // 创建具体工厂A        factoryA := &ConcreteFactoryA{}        // 使用工厂A创建产物A        productA := factoryA.CreateProduct()        // 使用产物A        println(productA.Use())        // 创建具体工厂B        factoryB := &ConcreteFactoryB{}        // 使用工厂B创建产物B        productB := factoryB.CreateProduct()        // 使用产物B        println(productB.Use())}
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

农民

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表