forked from go/golangs_learn
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1.3 KiB
1.3 KiB
package abstract_factory
import "fmt"
type Lunch interface {
Cook()
}
type Rise struct {
}
func (r *Rise) Cook() {
fmt.Println("这是 测试1111。")
}
type Tomato struct {
}
func (t *Tomato) Cook() {
fmt.Println("这是 测试22222。")
}
type LunchFactory interface {
CreateFood() Lunch
CreateVegetable() Lunch
}
type SimpleLunchFactory struct {
}
func NewSimpleLunchFactory() LunchFactory {
return &SimpleLunchFactory{}
}
func (s *SimpleLunchFactory) CreateFood() Lunch {
return &Rise{}
}
func (s *SimpleLunchFactory) CreateVegetable() Lunch {
return &Tomato{}
}
-
抽象工厂
-
实际抽象工厂是对工程的工厂函数的再次接口抽象
-
type Lunch interface { Cook() }
声明接口,声明type Rise struct {}
和type Tomato struct {}
结构体,通过结构体声明接口函数,使得实现接口,从而实现多态。 -
type LunchFactory interface { CreateFood() Lunch; CreateVegetable() Lunch }
声明一个接口,接口中存在不同函数,用于返回不同对象,是对 原 工厂模式 升级,使 -
通过工厂函数,通过调用不同的接口函数,调用不同接口实现,完成(接口)多态调用。而工厂函数返回实际对象,从而使用实际对象,调用接口具体实现。