-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathabstractfactory_robot.go
69 lines (55 loc) · 1.58 KB
/
abstractfactory_robot.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package abstractfactory
import "fmt"
//IRobot : 机器人能做的事情
type IRobot interface {
Name() string
DoWork()
//.....BALABALA..可以继续写很多接口方法
//每个接口方法做一件事
}
//IBattery 电池能做的事情
type IBattery interface {
Charge(robot IRobot)
//.....BALABALA..可以继续写很多接口方法
//每个接口方法做一件事
}
//IProduce 是当抽象工厂模式例子中的关键接口
//IProduce 返回一组产品对象
//IProduce 本质是创建工作对象,但必须以接口方式返回
type IProduce interface {
CreateRobot() IRobot
CreateBattery() IBattery
//.....BALABALA..可以继续写很多接口方法
//每个接口方法都要返回一个接口
}
////////////////////////////////
//接口定义好了,开始进行实现和应用
////////////////////////////////
//HomeRobot 家用机器人
type HomeRobot struct{}
//DoWork 机器人可以做工作
func (*HomeRobot) DoWork() {
fmt.Print("robot is cleaning home\n")
}
//Name 机器人的名字
func (*HomeRobot) Name() string {
return fmt.Sprint("home robot")
}
//HomeBattery 家用电池
type HomeBattery struct{}
// Charge SaveOrderDetail接口,保存订单细节
func (*HomeBattery) Charge(robot IRobot) {
rn := robot.Name()
fmt.Print("HomeBattery is charging for:", rn)
fmt.Println()
}
//HomeRobotFactory 家用机器人工厂
type HomeRobotFactory struct{}
//CreateRobot 创建机器人
func (*HomeRobotFactory) CreateRobot() IRobot {
return &HomeRobot{}
}
//CreateBattery 创建电池
func (*HomeRobotFactory) CreateBattery() IBattery {
return &HomeBattery{}
}