简介
简单工厂、工厂方法、抽象工厂、DI容器
简单工厂
由于Go本身没有构造函数,一般而言使用NewName的方式创建对象/接口,当它返回的是接口的时候,其实就是简单工厂模式
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}func main() {NewIRuleConfigParser("json")}
