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

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

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 动态的创建工厂。

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

相关推荐
Code blocks20 分钟前
使用Jenkins完成springboot项目快速更新
java·运维·spring boot·后端·jenkins
追逐时光者1 小时前
一款开源免费、通用的 WPF 主题控件包
后端·.net
蜗牛沐雨1 小时前
警惕 Rust 字符串的性能陷阱:`chars().nth()` 的深坑与高效之道
开发语言·后端·rust
&Sinnt&2 小时前
Git 版本控制完全指南:从入门到精通
git·后端
陈随易2 小时前
MoonBit助力前端开发,加密&性能两不误,斐波那契测试提高3-4倍
前端·后端·程序员
wfsm2 小时前
spring事件使用
java·后端·spring
微风粼粼3 小时前
程序员在线接单
java·jvm·后端·python·eclipse·tomcat·dubbo
缘来是庄3 小时前
设计模式之中介者模式
java·设计模式·中介者模式