论坛
潜水/灌水快乐,沉淀知识,认识更多同行。
ToB圈子
加入IT圈,遇到更多同好之人。
朋友圈
看朋友圈动态,了解ToB世界。
ToB门户
了解全球最新的ToB事件
博客
Blog
排行榜
Ranklist
文库
业界最专业的IT文库,上传资料也可以赚钱
下载
分享
Share
导读
Guide
相册
Album
记录
Doing
搜索
本版
文章
帖子
ToB圈子
用户
免费入驻
产品入驻
解决方案入驻
公司入驻
案例入驻
登录
·
注册
只需一步,快速开始
账号登录
立即注册
找回密码
用户名
Email
自动登录
找回密码
密码
登录
立即注册
首页
找靠谱产品
找解决方案
找靠谱公司
找案例
找对的人
专家智库
悬赏任务
圈子
SAAS
IT评测·应用市场-qidao123.com
»
论坛
›
数据库
›
Oracle
›
Golang学习条记_28——工厂方法模式
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(具体工厂):实现了工厂接口,返回具体产物的实例。
+-----------------+
| Product |<----------------+
+-----------------+ |
| + Use() | |
+-----------------+ |
^ |
| |
+-----------------+ +-----------------+
| ConcreteProductA| | ConcreteProductB|
+-----------------+ +-----------------+
| + Use() | | + Use() |
+-----------------+ +-----------------+
+-----------------+
| Creator |<----------------+
+-----------------+ |
| + CreateProduct()| |
+-----------------+ |
^ |
| |
+-----------------+ +-----------------+
| ConcreteCreatorA| | ConcreteCreatorB|
+-----------------+ +-----------------+
| + CreateProduct()| | + CreateProduct()|
+-----------------+ +-----------------+
复制代码
4. 实现
// 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{}
}
复制代码
源码
// 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())}
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复
使用道具
举报
0 个回复
倒序浏览
返回列表
快速回复
高级模式
B
Color
Image
Link
Quote
Code
Smilies
您需要登录后才可以回帖
登录
or
立即注册
本版积分规则
发表回复
回帖并转播
回帖后跳转到最后一页
发新帖
回复
农民
论坛元老
这个人很懒什么都没写!
楼主热帖
数据库入门
肝了五万字把SQL数据库从基础到高级所 ...
java反射大白话
iOS WebRTC 点对点实时音视频流程介绍 ...
Java中set集合简介说明
【R语言数据科学】(十二):有趣的概 ...
每日算法之数组中的逆序对
消息队列常见的使用场景
flume基本安装与使用
CentOS 7.9 安装 rocketmq-4.9.2
标签云
AI
运维
CIO
存储
服务器
浏览过的版块
Java
.Net
DevOps与敏捷开发
快速回复
返回顶部
返回列表