-
Notifications
You must be signed in to change notification settings - Fork 79
/
mediator.go
73 lines (60 loc) · 1.56 KB
/
mediator.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
package behavioral
import "fmt"
// WildStallion describes an interface for a Wild Stallion band member.
type WildStallion interface {
SetMediator(mediator Mediator)
}
// Bill describes Bill S. Preston, Esquire.
type Bill struct {
mediator Mediator
}
// SetMediator sets the mediator.
func (b *Bill) SetMediator(mediator Mediator) {
b.mediator = mediator
}
// Respond responds.
func (b *Bill) Respond() {
fmt.Fprintf(outputWriter, "Bill: What?\n")
b.mediator.Communicate("Bill")
}
// Ted describes Ted "Theodore" Logan.
type Ted struct {
mediator Mediator
}
// SetMediator sets the mediator.
func (t *Ted) SetMediator(mediator Mediator) {
t.mediator = mediator
}
// Talk talks through mediator.
func (t *Ted) Talk() {
fmt.Fprintf(outputWriter, "Ted: Bill?\n")
t.mediator.Communicate("Ted")
}
// Respond responds.
func (t *Ted) Respond() {
fmt.Fprintf(outputWriter, "Ted: Strange things are afoot at the Circle K.\n")
}
// Mediator describes the interface for communicating between Wild Stallion band members.
type Mediator interface {
Communicate(who string)
}
// ConcreateMediator describes a mediator between Bill and Ted.
type ConcreateMediator struct {
Bill
Ted
}
// NewMediator creates a new ConcreateMediator.
func NewMediator() *ConcreateMediator {
mediator := &ConcreateMediator{}
mediator.Bill.SetMediator(mediator)
mediator.Ted.SetMediator(mediator)
return mediator
}
// Communicate communicates between Bill and Ted.
func (m *ConcreateMediator) Communicate(who string) {
if who == "Ted" {
m.Bill.Respond()
} else if who == "Bill" {
m.Ted.Respond()
}
}