深入了解GoLang中的工廠設(shè)計模式
1. 定義
工廠模式是一種創(chuàng)建型設(shè)計模式,有了工廠只需要知道要制造的東西名字,就能讓對應工廠進行生產(chǎn),不用關(guān)心生產(chǎn)過程。
2. 優(yōu)點
1、一個調(diào)用者想創(chuàng)建一個對象,只要知道其名稱就可以了。
2、擴展性高,如果想增加一個產(chǎn)品,只要擴展一個工廠類就可以。
3、屏蔽產(chǎn)品的具體實現(xiàn),調(diào)用者只關(guān)心產(chǎn)品的接口。
3. 代碼實現(xiàn)
3.1 普通工廠
package factory
type HeroFactory interface {
Name(str string)
}
type WoManHero struct {
Age string
Hight float32
}
func (w WoManHero) Name(str string) {
panic("implement me")
}
type ManHero struct {
Age string
Hight float32
}
func (m ManHero) Name(str string) {
panic("implement me")
}
// 簡單工廠模式
func NewHeroFactory(sex int) HeroFactory {
switch sex {
case 0:
return ManHero{}
case 1:
return WoManHero{}
default:
return nil
}
}3.2 工廠方法
package factory
type IRuleHeroFactor interface {
CreateHero() HeroFactory
}
type WoManHeroFactory struct {
}
func (s WoManHeroFactory) CreateHero() HeroFactory {
return &WoManHero{}
}
type ManHeroFactory struct {
}
func (p ManHeroFactory) CreateHero() HeroFactory {
return &ManHero{}
}
// NewIRuleConfigParserFactory 用一個簡單工廠封裝工廠方法
func NewIRuleConfigParserFactory(t string) IRuleHeroFactor {
switch t {
case "M":
return ManHeroFactory{}
case "W":
return WoManHeroFactory{}
}
return nil
}3.3 抽象工廠
抽象接口里包含了各自的工廠方法或者普通工廠,再由各自實現(xiàn)自己的工廠
package factory
// IRuleConfigParser IRuleConfigParser
type IRuleConfigParser interface {
Parse(data []byte)
}
// jsonRuleConfigParser jsonRuleConfigParser
type jsonRuleConfigParser struct{}
// Parse Parse
func (j jsonRuleConfigParser) Parse(data []byte) {
panic("implement me")
}
// ISystemConfigParser ISystemConfigParser
type ISystemConfigParser interface {
ParseSystem(data []byte)
}
// jsonSystemConfigParser jsonSystemConfigParser
type jsonSystemConfigParser struct{}
// Parse Parse
func (j jsonSystemConfigParser) ParseSystem(data []byte) {
panic("implement me")
}
// IConfigParserFactory 工廠方法接口
type IConfigParserFactory interface {
CreateRuleParser() IRuleConfigParser
CreateSystemParser() ISystemConfigParser
}
type jsonConfigParserFactory struct{}
func (j jsonConfigParserFactory) CreateRuleParser() IRuleConfigParser {
return jsonRuleConfigParser{}
}
func (j jsonConfigParserFactory) CreateSystemParser() ISystemConfigParser {
return jsonSystemConfigParser{}
}IConfigParserFactory包含了IRuleConfigParser和ISystemConfigParser兩個解析器,再分別由jsonRuleConfigParser和jsonConfigParserFactory實現(xiàn)對應的解析方法
到此這篇關(guān)于深入了解GoLang中的工廠設(shè)計模式的文章就介紹到這了,更多相關(guān)GoLang工廠模式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Golang限流器time/rate設(shè)計與實現(xiàn)詳解
在?Golang?庫中官方給我們提供了限流器的實現(xiàn)golang.org/x/time/rate,它是基于令牌桶算法(Token?Bucket)設(shè)計實現(xiàn)的,下面我們就來看看他的具體使用吧2024-03-03
golang創(chuàng)建文件目錄os.Mkdir,os.MkdirAll的區(qū)別說明
本文主要講述os.Mkdir、os.MkdirAll區(qū)別以及在創(chuàng)建文件目錄過程中的一些其他技巧,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03
Go語言數(shù)據(jù)結(jié)構(gòu)之二叉樹可視化詳解
這篇文章主要為大家詳細介紹了Go語言數(shù)據(jù)結(jié)構(gòu)中二叉樹可視化的方法詳解,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下2022-09-09
Go語言中三個輸入函數(shù)(scanf,scan,scanln)的區(qū)別解析
本文詳細介紹了Go語言中三個輸入函數(shù)Scanf、Scan和Scanln的區(qū)別,包括用法、功能和輸入終止條件等,本文給大家介紹的非常詳細,感興趣的朋友跟隨小編一起看看吧2024-10-10

