深入了解GoLang中的工廠設(shè)計(jì)模式
1. 定義
工廠模式是一種創(chuàng)建型設(shè)計(jì)模式,有了工廠只需要知道要制造的東西名字,就能讓對(duì)應(yīng)工廠進(jìn)行生產(chǎn),不用關(guān)心生產(chǎn)過程。
2. 優(yōu)點(diǎn)
1、一個(gè)調(diào)用者想創(chuàng)建一個(gè)對(duì)象,只要知道其名稱就可以了。
2、擴(kuò)展性高,如果想增加一個(gè)產(chǎn)品,只要擴(kuò)展一個(gè)工廠類就可以。
3、屏蔽產(chǎn)品的具體實(shí)現(xiàn),調(diào)用者只關(guān)心產(chǎn)品的接口。
3. 代碼實(shí)現(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 用一個(gè)簡單工廠封裝工廠方法 func NewIRuleConfigParserFactory(t string) IRuleHeroFactor { switch t { case "M": return ManHeroFactory{} case "W": return WoManHeroFactory{} } return nil }
3.3 抽象工廠
抽象接口里包含了各自的工廠方法或者普通工廠,再由各自實(shí)現(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
兩個(gè)解析器,再分別由jsonRuleConfigParser
和jsonConfigParserFactory
實(shí)現(xiàn)對(duì)應(yīng)的解析方法
到此這篇關(guān)于深入了解GoLang中的工廠設(shè)計(jì)模式的文章就介紹到這了,更多相關(guān)GoLang工廠模式內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Golang使用Gin框架實(shí)現(xiàn)HTTP上傳文件過程介紹
由于需求中有文件上傳這一個(gè)需求,在這里我們就學(xué)習(xí)一下go語言如何上傳文件。本文主要通過表單的方式進(jìn)行文件上傳操作,本文實(shí)例為大家分享了Go實(shí)現(xiàn)文件上傳操作的具體代碼,供大家參考,具體內(nèi)容如下2023-04-04golang中判斷請(qǐng)求是http還是https獲取當(dāng)前訪問地址
這篇文章主要為大家介紹了golang中判斷請(qǐng)求是http還是https獲取當(dāng)前訪問地址示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10golang實(shí)現(xiàn)ftp實(shí)時(shí)傳輸文件的案例
這篇文章主要介紹了golang實(shí)現(xiàn)ftp實(shí)時(shí)傳輸文件的案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-12-12深入理解Golang之http server的實(shí)現(xiàn)
這篇文章主要介紹了深入理解Golang之http server的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11Go語言結(jié)合grpc和protobuf實(shí)現(xiàn)去中心化的聊天室
這篇文章主要為大家詳細(xì)介紹了Go語言如何結(jié)合grpc和protobuf實(shí)現(xiàn)去中心化的聊天室,文中的示例代碼講解詳細(xì),有需要的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-03