From 936f13d150e712c85649993541fe7bd3d2d79ac6 Mon Sep 17 00:00:00 2001 From: Edward Date: Fri, 24 Apr 2020 11:15:47 +0800 Subject: [PATCH] finsih factory_method --- creation/04_factory_method/factorymethod.go | 47 +++++++++++-------- .../04_factory_method/factorymethod_test.go | 27 ++++++----- 2 files changed, 41 insertions(+), 33 deletions(-) diff --git a/creation/04_factory_method/factorymethod.go b/creation/04_factory_method/factorymethod.go index 6a6f161..0c11014 100644 --- a/creation/04_factory_method/factorymethod.go +++ b/creation/04_factory_method/factorymethod.go @@ -1,6 +1,9 @@ package factorymethod -import "fmt" +import ( + "fmt" + "strconv" +) //Assistant 是robot能做的事情 type Assistant interface { @@ -17,23 +20,25 @@ type IRobotFactory interface { //BasicRobotModel 是基本的机器人模型 type BasicRobotModel struct { - words string - a, b int + words string + workTime int } //Clean 打扫 -func (o *BasicRobotModel) Clean(a int) { - fmt.Printf("%d", a) +func (b *BasicRobotModel) Clean(a int) { + b.workTime = a + fmt.Printf("i can clean :%d hours\n", a) } //Speak 说话 -func (o *BasicRobotModel) Speak(b int) { - o.b = b +func (b *BasicRobotModel) Speak(w string) { + b.words = w + fmt.Printf("my name is: %s\n", w) } //Work main work -func (o *BasicRobotModel) Work() string { - fmt.Sprint("my main work is do somthing") +func (b *BasicRobotModel) Work() string { + return fmt.Sprint("my main work is do somthing") } //FightingRobotFactory 生产各类军工机器人 @@ -41,8 +46,8 @@ type FightingRobotFactory struct{} //Build a robot from FightingRobotFactory func (FightingRobotFactory) Build() Assistant { - return &PlusOperator{ - OperatorBase: &OperatorBase{}, + return &FightingRobot{ + BasicRobotModel: &BasicRobotModel{}, } } @@ -51,9 +56,10 @@ type FightingRobot struct { *BasicRobotModel } -//Result 获取结果 -func (o FightingRobot) Result() int { - return o.a + o.b +//Work for FightingRobot to do some fighting +func (f FightingRobot) Work() string { + fmt.Printf("%s\n", "i can fighting") + return "i can fighting" + strconv.Itoa(f.workTime) } //HomeRobotFactory 生产各类家用机器人 @@ -61,17 +67,18 @@ type HomeRobotFactory struct{} //Build a robot from HomeRobotFactory func (HomeRobotFactory) Build() Assistant { - return &MinusOperator{ - OperatorBase: &OperatorBase{}, + return &HomeRobot{ + BasicRobotModel: &BasicRobotModel{}, } } //HomeRobot 实际的家用机器人 type HomeRobot struct { - *OperatorBase + *BasicRobotModel } -//Result 获取结果 -func (o HomeRobot) Result() int { - return o.a - o.b +//Work robot do some work +func (h HomeRobot) Work() string { + fmt.Printf("%s\n", "i can do homework") + return "i can do homework" + strconv.Itoa(h.workTime) } diff --git a/creation/04_factory_method/factorymethod_test.go b/creation/04_factory_method/factorymethod_test.go index 04085c4..d5e3afc 100644 --- a/creation/04_factory_method/factorymethod_test.go +++ b/creation/04_factory_method/factorymethod_test.go @@ -2,25 +2,26 @@ package factorymethod import "testing" -func compute(factory IRobotFactory, a, b int) int { - op := factory.Create() - op.SetA(a) - op.SetB(b) - return op.Result() +func doWork(factory IRobotFactory, cleanhour int) string { + robot := factory.Build() + robot.Clean(cleanhour) + + robot.Speak("robot name") + + return robot.Work() + } -func TestOperator(t *testing.T) { - var ( - factory OperatorFactory - ) +func TestRobotFactory(t *testing.T) { + var factory IRobotFactory - factory = PlusOperatorFactory{} - if compute(factory, 1, 2) != 3 { + factory = FightingRobotFactory{} + if doWork(factory, 2) != "i can fighting2" { t.Fatal("error with factory method pattern") } - factory = MinusOperatorFactory{} - if compute(factory, 4, 2) != 2 { + factory = HomeRobotFactory{} + if doWork(factory, 1) != "i can do homework1" { t.Fatal("error with factory method pattern") } }