-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathobserverv3.go
More file actions
76 lines (59 loc) · 1.36 KB
/
observerv3.go
File metadata and controls
76 lines (59 loc) · 1.36 KB
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
package ch14
type V3Subject interface {
Notify()
Attach(V3Observer)
GetAction() string
SetAction(string)
// Detach(Observer)
}
type V3Observer interface {
Update()
}
type V3SubSecretary struct {
observers []V3Observer
action string
}
func (s *V3SubSecretary) Notify() {
for _, o := range s.observers {
o.Update()
}
}
func (s *V3SubSecretary) Attach(o V3Observer) {
s.observers = append(s.observers, o)
}
func (s *V3SubSecretary) SetAction(action string) {
s.action = action
}
func (s *V3SubSecretary) GetAction() string {
return s.action
}
type V3StockObserver struct {
name string
sub V3Subject
}
func NewV3StockObserver(name string, sub V3Subject) *V3StockObserver {
return &V3StockObserver{name: name, sub: sub}
}
func (o *V3StockObserver) Update() {
println(o.name, "close stock, continue work", o.sub.GetAction())
}
type V3NBAObserver struct {
name string
sub V3Subject
}
func NewV3NBAObserver(name string, sub V3Subject) *V3NBAObserver {
return &V3NBAObserver{name: name, sub: sub}
}
func (o *V3NBAObserver) Update() {
println(o.name, "close NBA, continue work", o.sub.GetAction())
}
func ObserverV3() {
var sub V3Subject
sub = &V3SubSecretary{}
stockObserver := NewV3StockObserver("stock", sub)
nbaObserver := NewV3NBAObserver("nba", sub)
sub.Attach(stockObserver)
sub.Attach(nbaObserver)
sub.SetAction("boss is coming")
sub.Notify()
}