最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

深入了解GoLang中的工廠設(shè)計(jì)模式

 更新時(shí)間:2023年05月12日 08:32:37   作者:未來誰(shuí)可知  
這篇文章主要介紹了深入了解GoLang中的工廠設(shè)計(jì)模式,工廠模式是一種常用的設(shè)計(jì)模式,它屬于創(chuàng)建型模式,它的主要目的是封裝對(duì)象的創(chuàng)建過程,將對(duì)象的創(chuàng)建過程與對(duì)象的使用過程分離,從而提高代碼的可維護(hù)性和可擴(kuò)展性,需要詳細(xì)了解可以參考下文

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")
}
// 簡(jiǎn)單工廠模式
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è)簡(jiǎn)單工廠封裝工廠方法
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包含了IRuleConfigParserISystemConfigParser兩個(gè)解析器,再分別由jsonRuleConfigParserjsonConfigParserFactory實(shí)現(xiàn)對(duì)應(yīng)的解析方法

到此這篇關(guān)于深入了解GoLang中的工廠設(shè)計(jì)模式的文章就介紹到這了,更多相關(guān)GoLang工廠模式內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

米林县| 伽师县| 信阳市| 新安县| 江城| 武胜县| 永靖县| 成都市| 高要市| 郎溪县| 资兴市| 忻城县| 屏东县| 铁岭市| 东港市| 无棣县| 雅安市| 蓬安县| 丰都县| 中超| 浦县| 涪陵区| 平顶山市| 阿荣旗| 永丰县| 黔东| 洪洞县| 赤城县| 仙游县| 太原市| 桦南县| 潍坊市| 曲周县| 秭归县| 育儿| 宝坻区| 通城县| 奉新县| 河源市| 河池市| 凤翔县|