-
Notifications
You must be signed in to change notification settings - Fork 79
/
composite.go
46 lines (37 loc) · 987 Bytes
/
composite.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
package structural
import "fmt"
// Component describes the behavior that needs to be exercised uniformly
// across all primitive and composite objects.
type Component interface {
Traverse()
}
// Leaf describes a primitive leaf object in the hierarchy.
type Leaf struct {
value int
}
// NewLeaf creates a new leaf.
func NewLeaf(value int) *Leaf {
return &Leaf{value}
}
// Traverse prints the value of the leaf.
func (l *Leaf) Traverse() {
fmt.Printf("%v ", l.value)
}
// Composite describes a composite of components.
type Composite struct {
children []Component
}
// NewComposite creates a new composite.
func NewComposite() *Composite {
return &Composite{make([]Component, 0)}
}
// Add adds a new component to the composite.
func (c *Composite) Add(component Component) {
c.children = append(c.children, component)
}
// Traverse traverses the composites children.
func (c *Composite) Traverse() {
for i := 0; i < len(c.children); i++ {
c.children[i].Traverse()
}
}