设计模式: 抽象工厂模式练习
package main
import "fmt"
func main() {
baseWindow := NewBase(&Windows{})
fmt.Println(baseWindow.Opt.Jump())
baseWindow.Inter.Color()
baseAnd := NewBase(&Android{})
fmt.Println(baseAnd.Opt.Jump())
baseAnd.Inter.Color()
}
// 抽象工厂模式练习题2
// 某游戏厂商推出新的 手机游戏软件 支持 ios、android、windows 系统
// 针对不同的 系统。 提供不同的 游戏操作控制类 和 游戏界面控制类
// 创建方法的接口
type CreateFactory interface {
CreateOptimize() Optimize
CreateInterfaceControl() InterfaceControl
}
func NewBase(f CreateFactory) *Game {
return &Game{
Opt: f.CreateOptimize(),
Inter: f.CreateInterfaceControl(),
}
}
type Optimize interface {
Jump() string
}
type InterfaceControl interface {
Color()
}
type Opt struct {
Name string
}
func (o *Opt) Jump() string {
return o.Name
}
type Infc struct {
Name string
}
func (i *Infc) Color() {
fmt.Println(i.Name)
}
type Ios struct {}
func (is *Ios) CreateOptimize() Optimize {
return &Opt{Name: "ios opt"}
}
func (is *Ios) CreateInterfaceControl() InterfaceControl {
return &Infc{Name: "ios infc"}
}
type Android struct {}
func (ad *Android) CreateOptimize() Optimize {
return &Opt{Name: "and opt"}
}
func (ad *Android) CreateInterfaceControl() InterfaceControl {
return &Infc{Name: "and infd"}
}
type Windows struct {}
func (w *Windows) CreateOptimize() Optimize {
return &Opt{Name: "win opt"}
}
func (w *Windows) CreateInterfaceControl() InterfaceControl {
return &Infc{Name: "sin infc"}
}
type Game struct {
Opt Optimize
Inter InterfaceControl
}
邮箱: 1090055252@qq.com

浙公网安备 33010602011771号