SoFunction
Updated on 2025-03-01

Understand the factory design patterns in GoLang

1. Definition

The factory model is a creative design model. With a factory, you only need to know the name of the thing you want to manufacture, so that the corresponding factory can produce without caring about the production process.

2. Advantages

1. If a caller wants to create an object, just know its name.

2. High scalability. If you want to add a product, just expand a factory class.

3. Block the specific implementation of the product, and the caller only cares about the product's interface.

3. Code implementation

3.1 Ordinary factory

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")
}
// Simple factory modefunc NewHeroFactory(sex int) HeroFactory {
	switch sex {
	case 0:
		return ManHero{}
	case 1:
		return WoManHero{}
	default:
		return nil
	}
}

3.2 Factory Methods

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 uses a simple factory packaging factory methodfunc NewIRuleConfigParserFactory(t string) IRuleHeroFactor {
	switch t {
	case "M":
		return ManHeroFactory{}
	case "W":
		return WoManHeroFactory{}
	}
	return nil
}

3.3 Abstract factory

The abstract interface contains their own factory methods or ordinary factories, and then each implements their own factories.

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 factory method interfacetype IConfigParserFactory interface {
	CreateRuleParser() IRuleConfigParser
	CreateSystemParser() ISystemConfigParser
}
type jsonConfigParserFactory struct{}
func (j jsonConfigParserFactory) CreateRuleParser() IRuleConfigParser {
	return jsonRuleConfigParser{}
}
func (j jsonConfigParserFactory) CreateSystemParser() ISystemConfigParser {
	return jsonSystemConfigParser{}
}

IConfigParserFactoryIncludedIRuleConfigParserandISystemConfigParserThe two parsers are thenjsonRuleConfigParserandjsonConfigParserFactoryImplement the corresponding analysis method

This is the end of this article about in-depth understanding of the factory design model in GoLang. For more related content on GoLang factory model, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!