-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvisitor.go
78 lines (60 loc) · 1.01 KB
/
visitor.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
package main
import "fmt"
type Card interface {
GetTitle() string
GetPoints() int
}
type Visitable interface {
Accept(v Visitor)
}
type Task struct {
Title string
Time int
}
func (b *Task) GetTitle() string {
return b.Title
}
func (b *Task) GetPoints() int {
return b.Time
}
func (b *Task) Accept(v Visitor) {
v.Visit(b)
}
type Bug struct {
Title string
Time int
}
func (b *Bug) GetTitle() string {
return b.Title
}
func (b *Bug) GetPoints() int {
return b.Time
}
func (b *Bug) Accept(v Visitor) {
v.Visit(b)
}
type Visitor interface {
Visit(t Card)
}
type EstimationVisitor struct {
Sum int
}
func (e *EstimationVisitor) Visit(t Card) {
e.Sum += t.GetPoints()
}
func main() {
nextRelease := []Visitable{
&Task{"Do stuff", 1},
&Task{"Implement Foo Bar", 5},
&Bug{"Error 500 on resource /foo/bar", 3},
}
storyPoints := new(EstimationVisitor)
for _, i := range nextRelease {
i.Accept(storyPoints)
}
fmt.Println(
"Next release is calulated in",
storyPoints.Sum,
"story points",
)
}