-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate_method.go
110 lines (90 loc) · 2.54 KB
/
template_method.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package template_method
import (
"bytes"
"fmt"
)
// IActor 演员接口
type IActor interface {
DressUp() string // 装扮
}
// dressBehavior 装扮的多个行为,这里多个行为是私有的,通过DressUp模版方法调用
type dressBehavior interface {
makeUp() string // 化妆
clothe() string // 穿衣
wear() string // 配饰
}
// BaseActor 演员基类
type BaseActor struct {
roleName string // 扮演角色
dressBehavior // 装扮行为
}
// DressUp 统一实现演员接口的DressUp模版方法,装扮过程通过不同装扮行为进行扩展
func (b *BaseActor) DressUp() string {
buf := bytes.Buffer{}
buf.WriteString(fmt.Sprintf("扮演%s的", b.roleName))
buf.WriteString(b.makeUp())
buf.WriteString(b.clothe())
buf.WriteString(b.wear())
return buf.String()
}
// womanActor 扩展装扮行为的女演员
type womanActor struct {
BaseActor
}
// NewWomanActor 指定角色创建女演员
func NewWomanActor(roleName string) *womanActor {
actor := new(womanActor) // 创建女演员
actor.roleName = roleName // 设置角色
actor.dressBehavior = actor // 将女演员实现的扩展装扮行为,设置给自己的装扮行为接口
return actor
}
// 化妆
func (w *womanActor) makeUp() string {
return "女演员涂着口红,画着眉毛;"
}
// 穿衣
func (w *womanActor) clothe() string {
return "穿着连衣裙;"
}
// 配饰
func (w *womanActor) wear() string {
return "带着耳环,手拎着包;"
}
// manActor 扩展装扮行为的男演员
type manActor struct {
BaseActor
}
func NewManActor(roleName string) *manActor {
actor := new(manActor)
actor.roleName = roleName
actor.dressBehavior = actor // 将男演员实现的扩展装扮行为,设置给自己的装扮行为接口
return actor
}
func (m *manActor) makeUp() string {
return "男演员刮净胡子,抹上发胶;"
}
func (m *manActor) clothe() string {
return "穿着一身西装;"
}
func (m *manActor) wear() string {
return "带上手表,抽着烟;"
}
// NewChildActor 扩展装扮行为的儿童演员
type childActor struct {
BaseActor
}
func NewChildActor(roleName string) *childActor {
actor := new(childActor)
actor.roleName = roleName
actor.dressBehavior = actor // 将儿童演员实现的扩展装扮行为,设置给自己的装扮行为接口
return actor
}
func (c *childActor) makeUp() string {
return "儿童演员抹上红脸蛋;"
}
func (c *childActor) clothe() string {
return "穿着一身童装;"
}
func (c *childActor) wear() string {
return "手里拿着一串糖葫芦;"
}