You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

34 lines
552 B

package composite
import "fmt"
type Component interface {
Traverse()
}
type Leaf struct {
value int
}
func NewLeaf(value int) *Leaf {
return &Leaf{value: value}
}
func (l *Leaf) Traverse() {
fmt.Println(l.value)
}
type Composite struct {
children []Component
}
func NewComposite() *Composite {
return &Composite{children: make([]Component, 0)}
}
func (c *Composite) Add(component Component) {
c.children = append(c.children, component)
}
func (c *Composite) Traverse() {
for idx, _ := range c.children {
c.children[idx].Traverse()
}
}