深入了解GoLang中的工廠設(shè)計模式
1. 定義
工廠模式是一種創(chuàng)建型設(shè)計模式,有了工廠只需要知道要制造的東西名字,就能讓對應(yīng)工廠進行生產(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)對應(yīng)的解析方法
到此這篇關(guān)于深入了解GoLang中的工廠設(shè)計模式的文章就介紹到這了,更多相關(guān)GoLang工廠模式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Golang使用Gin框架實現(xiàn)HTTP上傳文件過程介紹
由于需求中有文件上傳這一個需求,在這里我們就學(xué)習(xí)一下go語言如何上傳文件。本文主要通過表單的方式進行文件上傳操作,本文實例為大家分享了Go實現(xiàn)文件上傳操作的具體代碼,供大家參考,具體內(nèi)容如下2023-04-04golang中判斷請求是http還是https獲取當(dāng)前訪問地址
這篇文章主要為大家介紹了golang中判斷請求是http還是https獲取當(dāng)前訪問地址示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-10-10深入理解Golang之http server的實現(xiàn)
這篇文章主要介紹了深入理解Golang之http server的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11Go語言結(jié)合grpc和protobuf實現(xiàn)去中心化的聊天室
這篇文章主要為大家詳細介紹了Go語言如何結(jié)合grpc和protobuf實現(xiàn)去中心化的聊天室,文中的示例代碼講解詳細,有需要的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-03