简单工厂模式
工厂接口
package simpleFactory
type drink interface{
show()
}
咖啡对象
package simpleFactory
type coffee struct {
}
func (c *coffee)show(){
fmt.Println("来杯咖啡提提神!")
}
茶对象
package simpleFactory
type tea struct{
}
func (t *tea)show(){
fmt.Println("来杯茶水养养生~")
}
简单工厂
package simpleFactory
func NewDrink(name string) drink{
switch name{
case "coffee":
return &coffee{}
case "tea":
return &tea{}
}
return nil
}
测试文件
package simpleFactory
func TestSimpleFactory(t *testing.T){
NewDrink("coffee").show()
NewDrink("tea").show()
}
标签:coffee,show,tea,package,模式,工厂,func,简单,simpleFactory
From: https://www.cnblogs.com/mathsmouse/p/16668241.html