01 golang 设计模式-创建型模-工厂方法
发布于 2022年 01月 16日 18:07
工厂方法 code
package factory
import "fmt"
type Company interface {
Goodjob()
}
type Thinry struct {
}
func (m *Thinry) Goodjob() {
fmt.Println("Thinry 9点开始进入工作")
}
type Shine struct {
}
func (m *Shine) Goodjob() {
fmt.Println("Shine 9点30开始进入工作")
}
func NewConpany(cType string) Company {
switch cType {
case "jit":
return &Thinry{}
case "shine":
return &Shine{}
}
return nil
}
Test code
package factory
import "testing"
func TestNewCompany(t *testing.T) {
NewConpany("jit").Goodjob()
NewConpany("shine").Goodjob()
}
Test执行结果
Thinry 9点开始进入工作
Shine 9点30开始进入工作