2. 工厂模式 #
2.1 简单工厂 #
使用 NewName 的方式创建对象/接口,当它返回的是接口的时候,其实就是简单工厂模式。
package factory
type IRuleConfigParser interface {
Parse(data []byte)
}
type jsonRuleConfigParser struct {
}
func (J jsonRuleConfigParser) Parse(data []byte) {
panic("implement me")
}
type yamlRuleConfigParser struct {
}
func (Y yamlRuleConfigParser) Parse(data []byte) {
panic("implement me")
}
func NewIRuleConfigParser(t string) IRuleConfigParser {
switch t {
case "json":
return jsonRuleConfigParser{}
case "yaml":
return yamlRuleConfigParser{}
}
return nil
}
2.2 工厂方法 #
当对象的创建逻辑比较复杂,不只是简单的 new 一下就可以,而是要组合其他类对象,做各种初始化操作的时候,推荐使用工厂方法模式,将复杂的创建逻辑拆分到多个工厂类中,让具体的工厂实现具体的业务逻辑,让每个工厂类都不至于过于复杂。
package factory
type IRuleConfigParser interface {
Parse(data []byte)
}
type jsonRuleConfigParser struct {
}
func (J jsonRuleConfigParser) Parse(data []byte) {
panic("implement me")
}
type yamlRuleConfigParser struct {
}
func (Y yamlRuleConfigParser) Parse(data []byte) {
panic("implement me")
}
type IRuleConfigParserFactory interface {
CreateParser() IRuleConfigParser
}
type yamlRuleConfigParserFactory struct {
}
func (y yamlRuleConfigParserFactory) CreateParser() IRuleConfigParser {
return yamlRuleConfigParser{}
}
type jsonRuleConfigParserFactory struct {
}
func (j jsonRuleConfigParserFactory) CreateParser() IRuleConfigParser {
return jsonRuleConfigParser{}
}
func NewIRuleConfigParserFactory(t string) IRuleConfigParserFactory {
switch t {
case "json":
return jsonRuleConfigParserFactory{}
case "yaml":
return yamlRuleConfigParserFactory{}
}
return nil
}
2.3 抽象工厂 #
一个工厂方法可以创建相关联的多个类的时候就是抽象工厂模式。
package factory
type IRuleConfigParser interface {
Parse(data []byte)
}
type jsonRuleConfigParser struct{}
func (j jsonRuleConfigParser) Parse(data []byte) {
panic("implement me")
}
type ISystemConfigParser interface {
ParseSystem(data []byte)
}
type jsonSystemConfigParser struct{}
func (j jsonSystemConfigParser) ParseSystem(data []byte) {
panic("implement me")
}
// 工厂方法接口
type IConfigParserFactory interface {
CreateRuleParser() IRuleConfigParser
CreateSystemParser() ISystemConfigParser
}
type jsonConfigParserFactory struct{}
func (j jsonConfigParserFactory) CreateRuleParser() IRuleConfigParser {
return jsonRuleConfigParser{}
}
func (j jsonConfigParserFactory) CreateSystemParser() ISystemConfigParser {
return jsonSystemConfigParser{}
}
type yamlConfigParserFactory struct{}
func (y yamlConfigParserFactory) CreateRuleParser() IRuleConfigParser {
return yamlRuleConfigParser{}
}
func (y yamlConfigParserFactory) CreateSystemParser() ISystemConfigParser {
return yamlSystemConfigParser{}
}
type yamlRuleConfigParser struct{}
func (y yamlRuleConfigParser) Parse(data []byte) {
panic("implement me")
}
type yamlSystemConfigParser struct{}
func (y yamlSystemConfigParser) ParseSystem(data []byte) {
panic("implement me")
}
// 抽象工厂基于工厂方法,用一个简单工厂封装工厂方法
// 只不过一个工厂方法可创建多个相关的类
func NewIConfigParserFactory(t string) IConfigParserFactory {
switch t {
case "json":
return jsonConfigParserFactory{}
case "yaml":
return yamlConfigParserFactory{}
}
return nil
}