设计模式: 抽象工厂模式练习

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
}

  

posted @ 2022-12-11 18:48  Black_Climber  阅读(33)  评论(0编辑  收藏  举报