-
Notifications
You must be signed in to change notification settings - Fork 14
/
visitor.go
81 lines (62 loc) · 1.52 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
package main
import "fmt"
type TaxiElement interface {
Accept(VisitorPassenger)
}
type Uber struct {
name string
}
func (self *Uber) Accept(visitor VisitorPassenger) {
self.name = "Uber"
visitor.CallUber(self)
}
type Lyft struct {
name string
}
func (self *Lyft) Accept(visitor VisitorPassenger) {
self.name = "Lyft"
visitor.CallLyft(self)
}
type Gojek struct {
name string
}
func (self *Gojek) Accept(visitor VisitorPassenger) {
self.name = "Gojek"
visitor.CallGojek(self)
}
type VisitorPassenger interface {
CallUber(*Uber)
CallLyft(*Lyft)
CallGojek(*Gojek)
}
type BusinessmanPassenger struct{}
func (self *BusinessmanPassenger) CallUber(uber *Uber) {
fmt.Println("Businessman is riding " + uber.name)
}
func (self *BusinessmanPassenger) CallLyft(lyft *Lyft) {
fmt.Println("Businessman is riding " + lyft.name)
}
func (self *BusinessmanPassenger) CallGojek(gojek *Gojek) {
fmt.Println("Businessman is riding " + gojek.name)
}
type StudentPassenger struct{}
func (self *StudentPassenger) CallUber(uber *Uber) {
fmt.Println("Student is riding " + uber.name)
}
func (self *StudentPassenger) CallLyft(lyft *Lyft) {
fmt.Println("Student is riding " + lyft.name)
}
func (self *StudentPassenger) CallGojek(gojek *Gojek) {
fmt.Println("Student is riding " + gojek.name)
}
func main() {
taxis := []TaxiElement{&Uber{}, &Lyft{}, &Gojek{}}
businessman := &BusinessmanPassenger{}
student := &StudentPassenger{}
for _, taxi := range taxis {
taxi.Accept(businessman)
}
for _, taxi := range taxis {
taxi.Accept(student)
}
}