Skip to content

Commit a0d7689

Browse files
committed
add composite
1 parent 9e704d1 commit a0d7689

File tree

3 files changed

+125
-0
lines changed

3 files changed

+125
-0
lines changed

13_composite/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# 组合模式
2+
3+
组合模式统一对象和对象集,使得使用相同接口使用对象和对象集。
4+
5+
组合模式常用于树状结构,用于统一叶子节点和树节点的访问,并且可以用于应用某一操作到所有子节点。

13_composite/composite.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package composite
2+
3+
import "fmt"
4+
5+
type Component interface {
6+
Parent() Component
7+
SetParent(Component)
8+
Name() string
9+
SetName(string)
10+
AddChild(Component)
11+
Print(string)
12+
}
13+
14+
const (
15+
LeafNode = iota
16+
CompositeNode
17+
)
18+
19+
func NewComponent(kind int, name string) Component {
20+
var c Component
21+
switch kind {
22+
case LeafNode:
23+
c = NewLeaf()
24+
case CompositeNode:
25+
c = NewComposite()
26+
}
27+
28+
c.SetName(name)
29+
return c
30+
}
31+
32+
type component struct {
33+
parent Component
34+
name string
35+
}
36+
37+
func (c *component) Parent() Component {
38+
return c.parent
39+
}
40+
41+
func (c *component) SetParent(parent Component) {
42+
c.parent = parent
43+
}
44+
45+
func (c *component) Name() string {
46+
return c.name
47+
}
48+
49+
func (c *component) SetName(name string) {
50+
c.name = name
51+
}
52+
53+
func (c *component) AddChild(Component) {}
54+
55+
func (c *component) Print(string) {}
56+
57+
type Leaf struct {
58+
component
59+
}
60+
61+
func NewLeaf() *Leaf {
62+
return &Leaf{}
63+
}
64+
65+
func (c *Leaf) Print(pre string) {
66+
fmt.Printf("%s-%s\n", pre, c.Name())
67+
}
68+
69+
type Composite struct {
70+
component
71+
childs []Component
72+
}
73+
74+
func NewComposite() *Composite {
75+
return &Composite{
76+
childs: make([]Component, 0),
77+
}
78+
}
79+
80+
func (c *Composite) AddChild(child Component) {
81+
child.SetParent(c)
82+
c.childs = append(c.childs, child)
83+
}
84+
85+
func (c *Composite) Print(pre string) {
86+
fmt.Printf("%s+%s\n", pre, c.Name())
87+
pre += " "
88+
for _, comp := range c.childs {
89+
comp.Print(pre)
90+
}
91+
}

13_composite/composite_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package composite
2+
3+
func ExampleComposite() {
4+
root := NewComponent(CompositeNode, "root")
5+
c1 := NewComponent(CompositeNode, "c1")
6+
c2 := NewComponent(CompositeNode, "c2")
7+
c3 := NewComponent(CompositeNode, "c3")
8+
9+
l1 := NewComponent(LeafNode, "l1")
10+
l2 := NewComponent(LeafNode, "l2")
11+
l3 := NewComponent(LeafNode, "l3")
12+
13+
root.AddChild(c1)
14+
root.AddChild(c2)
15+
c1.AddChild(c3)
16+
c1.AddChild(l1)
17+
c2.AddChild(l2)
18+
c2.AddChild(l3)
19+
20+
root.Print("")
21+
// Output:
22+
// +root
23+
// +c1
24+
// +c3
25+
// -l1
26+
// +c2
27+
// -l2
28+
// -l3
29+
}

0 commit comments

Comments
 (0)