-
Notifications
You must be signed in to change notification settings - Fork 246
/
chain_of_responsibility.go
52 lines (45 loc) · 1.15 KB
/
chain_of_responsibility.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
// Package chain_of_responsibility is an example of the Chain Of Responsibility Pattern.
package chain_of_responsibility
// Handler provides a handler interface.
type Handler interface {
SendRequest(message int) string
}
// ConcreteHandlerA implements handler "A".
type ConcreteHandlerA struct {
next Handler
}
// SendRequest implementation.
func (h *ConcreteHandlerA) SendRequest(message int) (result string) {
if message == 1 {
result = "Im handler 1"
} else if h.next != nil {
result = h.next.SendRequest(message)
}
return
}
// ConcreteHandlerB implements handler "B".
type ConcreteHandlerB struct {
next Handler
}
// SendRequest implementation.
func (h *ConcreteHandlerB) SendRequest(message int) (result string) {
if message == 2 {
result = "Im handler 2"
} else if h.next != nil {
result = h.next.SendRequest(message)
}
return
}
// ConcreteHandlerC implements handler "C".
type ConcreteHandlerC struct {
next Handler
}
// SendRequest implementation.
func (h *ConcreteHandlerC) SendRequest(message int) (result string) {
if message == 3 {
result = "Im handler 3"
} else if h.next != nil {
result = h.next.SendRequest(message)
}
return
}