forked from qq570850096/awesome-golang-DesignPattern
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComponent.go
123 lines (99 loc) · 2.28 KB
/
Component.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
111
112
113
114
115
116
117
118
119
120
121
122
package StructuralType
import "fmt"
type MenuComponent interface {
// 采购设备或者添加子部门
Add(menuComponent MenuComponent)
Remove(menuComponent MenuComponent)
// 查询该节点下所有设备和部门
GetName() string
GetPrice() float64
GetDescription() string
IsVegetarian() bool
CreateIterator()
Display(depth int)
}
type Leaf struct {
name string
vegetarian bool
description string
price float64
}
func (l *Leaf) Add(menuComponent MenuComponent) {
panic("Leaf Node can not add")
}
func (l *Leaf) Remove(menuComponent MenuComponent) {
panic("Leaf Node can not remove")
}
func (l *Leaf) GetName() string {
return l.name
}
func (l *Leaf) GetPrice() float64 {
return l.price
}
func (l *Leaf) IsVegetarian() bool {
return l.vegetarian
}
func (l *Leaf) GetDescription() string {
return l.description
}
func (l *Leaf) CreateIterator() {
panic("implement me")
}
func (l *Leaf) Display(depth int) {
// 输出树形结构的叶子结点,这里直接输出设备名
for i:=0; i<depth; i++ {
fmt.Print("*")
}
fmt.Println(l.Name())
}
func (l *Leaf) Name() string {
return l.name
}
func (l *Leaf) SetName(name string) {
l.name = name
}
// 复合构件
type Composite struct {
name string
description string
arr []MenuComponent
}
func (c *Composite) GetName() string {
return c.name
}
func (c *Composite) GetPrice() float64 {
panic("It is not an item.")
}
func (c *Composite) GetDescription() string {
return c.description
}
func (c *Composite) IsVegetarian() bool {
panic("implement me")
}
func (c *Composite) CreateIterator() {
panic("implement me")
}
func (c *Composite) Add(MenuComponent MenuComponent) {
c.arr = append(c.arr,MenuComponent)
}
func (c *Composite) Remove(MenuComponent MenuComponent) {
for i,v := range c.arr {
if v == MenuComponent {
// 删除第i个元素,因为interface类型在golang中
// 以地址的方式传递,所以可以直接比较进行删除
// golang中只要记得byte,int,bool,string,数组,结构体,默认传值,其他的默认传地址即可
c.arr = append(c.arr[:i],c.arr[i+1:]...)
}
}
}
func (c *Composite) Display(depth int) {
// 输出树形结构
for i:=0; i<depth; i++ {
fmt.Print("*")
}
fmt.Println(c.GetName())
// 递归显示
for _,com := range c.arr {
com.Display(depth+1)
}
}