-
Notifications
You must be signed in to change notification settings - Fork 0
/
chain_of_responsibility.go
55 lines (44 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
53
54
55
package behavioural
import "fmt"
/*
Summary:
Chain of Responsilbility pattern is used when a work is passed to a chain
of handlers until one of the processes it. The handlers can break the chain
and return as well.
Example:
Alert escalation policy follows a chain. If the alert is resolved by the first
one in chain, its done, otherwise next is tried. Finally the escaltion timesout.
Benefit:
- The client is decopupled from a series of task as it chains only to one
and then follows the chain to execute many.
- Design pattern to add series of tasks as chains of tasks.
- Easier to add new task, easier to write unit test for each task.
*/
type EscalationHandler interface {
Handle(alert string)
}
type escalation struct {
user string
canHandle []string
next EscalationHandler
}
func NewEscalation(
u string,
c []string,
h EscalationHandler,
) EscalationHandler {
return &escalation{user: u, canHandle: c, next: h}
}
func (e *escalation) Handle(alert string) {
for _, a := range e.canHandle {
if a == alert {
fmt.Printf("resolved by %s", e.user)
return
}
}
if e.next == nil {
fmt.Printf("timedout")
return
}
e.next.Handle(alert)
}