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

Golang学习笔记_25------协程
Golang学习笔记_26------通道
Golang学习笔记_27------单例模式


文章目录

    • 工厂方法模式
      • [1. 介绍](#1. 介绍)
      • [2. 优点](#2. 优点)
      • [3. 类图](#3. 类图)
      • [4. 实现](#4. 实现)
    • 源码

工厂方法模式

1. 介绍

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

2. 优点

  1. 解耦:将对象的创建与使用分离,客户端不需要知道具体的类名。

  2. 扩展性:增加新的产品类时,只需添加相应的工厂类即可,符合开闭原则。

  3. 单一职责原则:将创建对象的代码封装在工厂中,职责单一。

3. 类图

  1. Product(产品接口):定义了产品的接口,所有具体产品都要实现这个接口。

  2. ConcreteProduct(具体产品):实现了Product接口的具体类。

  3. Creator(工厂接口):声明了工厂方法,用于返回一个Product对象。

  4. ConcreteCreator(具体工厂):实现了工厂接口,返回具体产品的实例。

     +-----------------+
     |     Product     |<----------------+
     +-----------------+                 |
     | + Use()         |                 |
     +-----------------+                 |
             ^                           |
             |                           |
     +-----------------+         +-----------------+
     | ConcreteProductA|         | ConcreteProductB|
     +-----------------+         +-----------------+
     | + Use()         |         | + Use()         |
     +-----------------+         +-----------------+
    
     +-----------------+
     |     Creator     |<----------------+
     +-----------------+                 |
     | + CreateProduct()|                |
     +-----------------+                 |
             ^                           |
             |                           |
     +-----------------+         +-----------------+
     | ConcreteCreatorA|         | ConcreteCreatorB|
     +-----------------+         +-----------------+
     | + CreateProduct()|        | + CreateProduct()|
     +-----------------+         +-----------------+
    

4. 实现

go 复制代码
// Product 是产品接口
type Product interface {
	Use() string
}

// ConcreteProductA 是具体产品A
type ConcreteProductA struct{}

func (p *ConcreteProductA) Use() string {
	return "使用产品A"
}

// ConcreteProductB 是具体产品B
type ConcreteProductB struct{}

func (p *ConcreteProductB) Use() string {
	return "使用产品B"
}

// Factory 是工厂接口
type Factory interface {
	CreateProduct() Product
}

// ConcreteFactoryA 是具体工厂A
type ConcreteFactoryA struct{}

func (f *ConcreteFactoryA) CreateProduct() Product {
	return &ConcreteProductA{}
}

// ConcreteFactoryB 是具体工厂B
type ConcreteFactoryB struct{}

func (f *ConcreteFactoryB) CreateProduct() Product {
	return &ConcreteProductB{}
}

源码

go 复制代码
// Product 是产品接口
type Product interface {
	Use() string
}

// ConcreteProductA 是具体产品A
type ConcreteProductA struct{}

func (p *ConcreteProductA) Use() string {
	return "使用产品A"
}

// ConcreteProductB 是具体产品B
type ConcreteProductB struct{}

func (p *ConcreteProductB) Use() string {
	return "使用产品B"
}

// Factory 是工厂接口
type Factory interface {
	CreateProduct() Product
}

// ConcreteFactoryA 是具体工厂A
type ConcreteFactoryA struct{}

func (f *ConcreteFactoryA) CreateProduct() Product {
	return &ConcreteProductA{}
}

// ConcreteFactoryB 是具体工厂B
type ConcreteFactoryB struct{}

func (f *ConcreteFactoryB) CreateProduct() Product {
	return &ConcreteProductB{}
}

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())
}
相关推荐
Long_poem11 分钟前
【自学笔记】Spring Boot框架技术基础知识点总览-持续更新
spring boot·笔记·后端
YXWik61 小时前
23种设计模式
java·设计模式
攻城狮7号1 小时前
【第三节】C++设计模式(创建型模式)-单例模式
c++·单例模式·设计模式
贩卖纯净水.1 小时前
REACT学习DAY02(恨连接不上服务器)
服务器·学习·react.js
朗迹 - 张伟1 小时前
Golang连接使用SqlCipher
开发语言·后端·golang
南风过闲庭2 小时前
操作系统研究
大数据·人工智能·科技·学习·ai·系统架构
Niuguangshuo2 小时前
Python 单例模式笔记
笔记·python·单例模式
zh路西法3 小时前
【C++委托与事件】函数指针,回调机制,事件式编程与松耦合的设计模式(上)
开发语言·c++·观察者模式·设计模式
ox00803 小时前
C++ 设计模式-备忘录模式
c++·设计模式·备忘录模式
強云3 小时前
23种设计模式 - 备忘录模式
设计模式·备忘录模式