-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathvisitor.go
95 lines (75 loc) · 1.84 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package behavioral
import "fmt"
// Element defines an interface for accepting visitors.
type Element interface {
Accept(v Visitor)
}
// This defines a struct which is an element.
type This struct {
}
// This returns 'This' as a string.
func (t *This) This() string {
return "This"
}
// Accept accepts a visitor.
func (t *This) Accept(v Visitor) {
v.VisitThis(t)
}
// That defines a struct which is an element.
type That struct {
}
// That returns 'That' as a string.
func (t *That) That() string {
return "That"
}
// Accept accepts a visitor.
func (t *That) Accept(v Visitor) {
v.VisitThat(t)
}
// TheOther defines a struct which is an element.
type TheOther struct {
}
// TheOther returns 'TheOther' as a string.
func (t *TheOther) TheOther() string {
return "TheOther"
}
// Accept accepts a visitor.
func (t *TheOther) Accept(v Visitor) {
v.VisitTheOther(t)
}
// Visitor defines an interface for visiting this, that and the other.
type Visitor interface {
VisitThis(e *This)
VisitThat(e *That)
VisitTheOther(e *TheOther)
}
// UpVisitor defines an up visitor.
type UpVisitor struct {
}
// VisitThis visits this.
func (v *UpVisitor) VisitThis(e *This) {
fmt.Printf("do Up on %v\n", e.This())
}
// VisitThat visits that.
func (v *UpVisitor) VisitThat(e *That) {
fmt.Printf("do Up on %v\n", e.That())
}
// VisitTheOther visits the other.
func (v *UpVisitor) VisitTheOther(e *TheOther) {
fmt.Printf("do Up on %v\n", e.TheOther())
}
// DownVisitor defines a down visitor.
type DownVisitor struct {
}
// VisitThis visits this.
func (v *DownVisitor) VisitThis(e *This) {
fmt.Printf("do Down on %v\n", e.This())
}
// VisitThat visits that.
func (v *DownVisitor) VisitThat(e *That) {
fmt.Printf("do Down on %v\n", e.That())
}
// VisitTheOther visits the other.
func (v *DownVisitor) VisitTheOther(e *TheOther) {
fmt.Printf("do Down on %v\n", e.TheOther())
}