2020-04-21 17:50:21 +03:00
|
|
|
package factorymethod
|
|
|
|
|
2020-04-23 07:23:28 +03:00
|
|
|
import "fmt"
|
|
|
|
|
|
|
|
//Assistant 是robot能做的事情
|
|
|
|
type Assistant interface {
|
|
|
|
Clean(int)
|
|
|
|
Speak(string)
|
|
|
|
Work() string
|
2020-04-21 17:50:21 +03:00
|
|
|
}
|
|
|
|
|
2020-04-23 07:23:28 +03:00
|
|
|
//IRobotFactory must be implemented by Factory
|
|
|
|
//different Factory create different robot
|
|
|
|
type IRobotFactory interface {
|
|
|
|
Build() Assistant
|
2020-04-21 17:50:21 +03:00
|
|
|
}
|
|
|
|
|
2020-04-23 07:23:28 +03:00
|
|
|
//BasicRobotModel 是基本的机器人模型
|
|
|
|
type BasicRobotModel struct {
|
|
|
|
words string
|
|
|
|
a, b int
|
2020-04-21 17:50:21 +03:00
|
|
|
}
|
|
|
|
|
2020-04-23 07:23:28 +03:00
|
|
|
//Clean 打扫
|
|
|
|
func (o *BasicRobotModel) Clean(a int) {
|
|
|
|
fmt.Printf("%d", a)
|
2020-04-21 17:50:21 +03:00
|
|
|
}
|
|
|
|
|
2020-04-23 07:23:28 +03:00
|
|
|
//Speak 说话
|
|
|
|
func (o *BasicRobotModel) Speak(b int) {
|
2020-04-21 17:50:21 +03:00
|
|
|
o.b = b
|
|
|
|
}
|
|
|
|
|
2020-04-23 07:23:28 +03:00
|
|
|
//Work main work
|
|
|
|
func (o *BasicRobotModel) Work() string {
|
|
|
|
fmt.Sprint("my main work is do somthing")
|
|
|
|
}
|
|
|
|
|
|
|
|
//FightingRobotFactory 生产各类军工机器人
|
|
|
|
type FightingRobotFactory struct{}
|
2020-04-21 17:50:21 +03:00
|
|
|
|
2020-04-23 07:23:28 +03:00
|
|
|
//Build a robot from FightingRobotFactory
|
|
|
|
func (FightingRobotFactory) Build() Assistant {
|
2020-04-21 17:50:21 +03:00
|
|
|
return &PlusOperator{
|
|
|
|
OperatorBase: &OperatorBase{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-23 07:23:28 +03:00
|
|
|
//FightingRobot 实际的战斗机器人
|
|
|
|
type FightingRobot struct {
|
|
|
|
*BasicRobotModel
|
2020-04-21 17:50:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//Result 获取结果
|
2020-04-23 07:23:28 +03:00
|
|
|
func (o FightingRobot) Result() int {
|
2020-04-21 17:50:21 +03:00
|
|
|
return o.a + o.b
|
|
|
|
}
|
|
|
|
|
2020-04-23 07:23:28 +03:00
|
|
|
//HomeRobotFactory 生产各类家用机器人
|
|
|
|
type HomeRobotFactory struct{}
|
2020-04-21 17:50:21 +03:00
|
|
|
|
2020-04-23 07:23:28 +03:00
|
|
|
//Build a robot from HomeRobotFactory
|
|
|
|
func (HomeRobotFactory) Build() Assistant {
|
2020-04-21 17:50:21 +03:00
|
|
|
return &MinusOperator{
|
|
|
|
OperatorBase: &OperatorBase{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-23 07:23:28 +03:00
|
|
|
//HomeRobot 实际的家用机器人
|
|
|
|
type HomeRobot struct {
|
2020-04-21 17:50:21 +03:00
|
|
|
*OperatorBase
|
|
|
|
}
|
|
|
|
|
|
|
|
//Result 获取结果
|
2020-04-23 07:23:28 +03:00
|
|
|
func (o HomeRobot) Result() int {
|
2020-04-21 17:50:21 +03:00
|
|
|
return o.a - o.b
|
|
|
|
}
|