【设计模式】已有工厂模式,抽象工厂改进了哪些?

抽象工厂模式是创建型的设计模式

GoF定义:提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。

优点和上一篇工厂模式提到的一样,那你会说都一样,为什么需要这个模式呢,所以肯定有不一样的地方。

业务是非常复杂的,一个流程会牵涉到多个对象,分多个对象就是为了体现单一职责设计思想,所以不会把一个流程中所有的功能都放到一个对象中。

抽象工厂模式就是为了解决这个问题的,它可以返回多个对象。具体来看示例。

场景

消息发送系统,需要支持 短信、邮件、APP推送、微信小程序订阅消息、微信服务号模板消息。

需要对接 AliYun、BaiduYun、容联云、微信。

go 复制代码
package main  
  
import "fmt"  

type ISms interface {
	Sms(msg string) (string, error)
}

type IAppPush interface {
	AppPush(msg string) (string, error)
}

type IEmail interface {
	Email(msg string) (string, error)
}

type IMiniProgramSubscribeMsg interface {
	SubscribeMsg(msg string) (string, error)
}

type IOfficeWeChatTemplateMsg interface {
	TemplateMsg(msg string) (string, error)
}

type IMessage interface {
	ISms
	IAppPush
	IEmail
	IMiniProgramSubscribeMsg
	IOfficeWeChatTemplateMsg
}

// AliYun 阿里云
type AliYun struct {
}

func NewAliYun() IMessage {
	return &AliYun{}
}

func (a *AliYun) Sms(msg string) (string, error) {
	return "AliYun sms send success, content: " + msg, nil
}

func (a *AliYun) AppPush(msg string) (string, error) {
	return "AliYun app push send success, content: " + msg, nil
}

func (a *AliYun) Email(msg string) (string, error) {
	return "AliYun email send success, content: " + msg, nil
}

func (a *AliYun) SubscribeMsg(msg string) (string, error) {
	return "", fmt.Errorf("miniProgram subscribe msg not support")
}

func (a *AliYun) TemplateMsg(msg string) (string, error) {
	return "", fmt.Errorf("office wechat not support")
}

// MiniProgram 小程序
type MiniProgram struct {
}

func NewMiniProgram() IMessage {
	return &MiniProgram{}
}

func (m *MiniProgram) Sms(msg string) (string, error) {
	return "", fmt.Errorf("sms not support")
}

func (m *MiniProgram) Email(msg string) (string, error) {
	return "", fmt.Errorf("email not support")
}

func (m *MiniProgram) AppPush(msg string) (string, error) {
	return "", fmt.Errorf("app push not support")
}

func (m *MiniProgram) SubscribeMsg(msg string) (string, error) {
	return "MiniProgram subscribe msg send success, content:" + msg, nil
}

func (m *MiniProgram) TemplateMsg(msg string) (string, error) {
	return "", fmt.Errorf("office wechat template msg not support")
}

type IMessageFactory interface {
	CreateSms() ISms
	CreateAppPush() IAppPush
	CreateEmail() IEmail
	CreateMiniProgramSubscribeMsg() IMiniProgramSubscribeMsg
	CreateIOfficeWeChatTemplateMsg() IOfficeWeChatTemplateMsg
}

// AliYunFactory 阿里云工厂
type AliYunFactory struct{}

func NewAliYunFactory() IMessageFactory {
	return &AliYunFactory{}
}

func (a *AliYunFactory) CreateSms() ISms {
	return NewAliYun()
}

func (a *AliYunFactory) CreateAppPush() IAppPush {
	return NewAliYun()
}

func (a *AliYunFactory) CreateEmail() IEmail {
	return NewAliYun()
}
func (a *AliYunFactory) CreateMiniProgramSubscribeMsg() IMiniProgramSubscribeMsg {
	return NewAliYun()
}

func (a *AliYunFactory) CreateIOfficeWeChatTemplateMsg() IOfficeWeChatTemplateMsg {
	return NewAliYun()
}

// MiniProgramFactory 小程序工厂
type MiniProgramFactory struct{}

func NewMiniProgramFactory() IMessageFactory {
	return &MiniProgramFactory{}
}

func (m *MiniProgramFactory) CreateSms() ISms {
	return NewMiniProgram()
}

func (m *MiniProgramFactory) CreateAppPush() IAppPush {
	return NewMiniProgram()
}

func (m *MiniProgramFactory) CreateEmail() IEmail {
	return NewMiniProgram()
}

func (m *MiniProgramFactory) CreateMiniProgramSubscribeMsg() IMiniProgramSubscribeMsg {
	return NewMiniProgram()
}

func (m *MiniProgramFactory) CreateIOfficeWeChatTemplateMsg() IOfficeWeChatTemplateMsg {
	return NewMiniProgram()
}

func main() {  
    message := "message content"  
    aliYunFactory := NewAliYunFactory()  
    fmt.Println(aliYunFactory.CreateSms().Sms(message))  
    fmt.Println(aliYunFactory.CreateAppPush().AppPush(message))  
    fmt.Println(aliYunFactory.CreateMiniProgramSubscribeMsg().SubscribeMsg(message))  
  
    baiduFactory := NewMiniProgramFactory()  
    fmt.Println(baiduFactory.CreateMiniProgramSubscribeMsg().SubscribeMsg(message))  
    fmt.Println(baiduFactory.CreateSms().Sms(message))  
}

NewAliYunFactory NewMiniProgramFactory 分别实现了 aliyun 的短信、邮件、APP推送、微信小程序订阅消息、微信服务号模板消息 对象创建 miniProgram 的短信、邮件、APP推送、微信小程序订阅消息、微信服务号模板消息 对象创建 然后分别调用 对应的方法进行数据发送

总结

看了上述的代码,可能会提出这样的问题 aliYunFactory、 baiduFactory 创建需要自己显示的指名,能否通过业务自动判断需要使用哪个工厂进行对象的创建? 答案是可以的,下面是修改后的代码

go 复制代码
type MessageFactoryFunc func() IMessageFactory  
  
var messageFactoryFuncMap = map[string]MessageFactoryFunc{  
    "aliyun":      NewAliYunFactory,  
    "miniprogram": NewMiniProgramFactory,  
}

func main() {
	message := "message content"  
	messageFactoryFunc := messageFactoryFuncMap["aliyun"]  
	factory := messageFactoryFunc()  
	fmt.Println(factory.CreateSms().Sms(message))  
	fmt.Println(factory.CreateAppPush().AppPush(message))  
	fmt.Println(factory.CreateMiniProgramSubscribeMsg().SubscribeMsg(message))
}

定义了 messageFactoryFuncMap 维护key和工厂方法的映射,这样就可以通过key 动态的创建工厂。

怎么样,有什么感想,我看了之后,感觉比直接写对应的方法实现复杂了不少,但是回过头来想一想,后续若需要新增新的服务商,会很方便。

相关推荐
小码哥_常23 分钟前
Java后端定时任务抉择:@Scheduled、Quartz、XXL - Job终极对决
后端
uzong28 分钟前
Skill 被广泛应用,到底什么是 Skill,今天详细介绍一下
人工智能·后端·面试
小码哥_常41 分钟前
Kafka平替!SpringBoot+Redis Stream+消费组打造极致消息队列
后端
IT_陈寒2 小时前
Redis缓存击穿:3个鲜为人知的防御策略,90%开发者都忽略了!
前端·人工智能·后端
uzong3 小时前
Harness Engineering 是什么?一场新的 AI 范式已经开始
人工智能·后端·架构
唐叔在学习3 小时前
Python桌面端应用最小化托盘开发实践
后端·python·程序员
yuhaiqiang4 小时前
被 AI 忽悠后,开始怀念搜索引擎了?
前端·后端·面试
二闹4 小时前
Python文件读取三巨头你该选择哪一个?
后端·python
苏三说技术4 小时前
推荐几个牛逼的AI Agent项目
后端
武子康5 小时前
大数据-253 离线数仓 - Airflow 入门与任务调度实战:DAG、Operator、Executor 部署排错指南
大数据·后端·apache hive