-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathresponsibility_chain.go
150 lines (123 loc) · 3.32 KB
/
responsibility_chain.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package chain
import "fmt"
////////////////////////////////
//使用费用申请审批的例子
//FeeRequest 这就要要处理的对象
type FeeRequest struct {
Mount int //申请的金额
RequiredLevel int //审批人等级要求
Name string //申请人
}
//IApprove 审批动作
type IApprove interface {
SetNext(next IApprove) //设置下一个审批流转
HaveRight(level int) bool //
HandleApproval(request FeeRequest) bool
}
////////////////////////////////
//实现方式1
////////////////////////////////
//FeeRequestChainFlow 定义一个流程管理器
type FeeRequestChainFlow struct {
approvers []IApprove //下一个流程节点
}
//AddApprover 添加一个审批对象
func (f *FeeRequestChainFlow) AddApprover(approver IApprove) {
f.approvers = append(f.approvers, approver)
}
//RunApprovalFlow to deal request by chains
func (f *FeeRequestChainFlow) RunApprovalFlow(request FeeRequest) {
for i := 0; i < len(f.approvers); i++ {
result := f.approvers[i].HandleApproval(request)
if !result {
//中间有一个环节出问题,流程就终止
break
}
}
}
////////////////////////////////
//实现方式2
////////////////////////////////
//GM 总经理要审批
type GM struct {
nextHandler IApprove //下一个流程节点
level int
}
//NewGM 总经理审批
func NewGM() IApprove {
return &GM{level: 8}
}
//SetNext 设置下一个审批节点
func (g *GM) SetNext(next IApprove) {
g.nextHandler = next
}
//HaveRight 处理审批所需要的权限级别
func (g *GM) HaveRight(RequiredLevel int) bool {
return g.level > RequiredLevel
}
//HandleApproval 进行审批
func (g *GM) HandleApproval(request FeeRequest) bool {
if g.HaveRight(request.RequiredLevel) {
fmt.Printf("GM permit %s %d fee request\n", request.Name, request.Mount)
return true
}
fmt.Printf("GM NO right to approve %s %d fee request\n", request.Name, request.Mount)
//direct forward to Next One
if g.nextHandler != nil {
return g.nextHandler.HandleApproval(request)
}
return true
}
//CFO 需要审批
type CFO struct {
nextHandler IApprove //下一个流程节点
level int
}
//NewCFO 对象
func NewCFO() IApprove {
return &CFO{}
}
//HaveRight CFO总是有权限的
func (*CFO) HaveRight(RequiredLevel int) bool {
return true
}
//SetNext 设置下一个审批节点
func (c *CFO) SetNext(next IApprove) {
c.nextHandler = next
}
//HandleApproval 进行审批
func (c *CFO) HandleApproval(request FeeRequest) bool {
if request.Mount < 1e+10 {
fmt.Printf("CFO permit %s %d fee request\n", request.Name, request.Mount)
return true
}
fmt.Printf("CFO No right to approve %s %d fee request \n", request.Name, request.Mount)
if c.nextHandler != nil {
return c.nextHandler.HandleApproval(request)
}
return true
}
//CEO 需要审批
type CEO struct {
}
//NewCEO 对象
func NewCEO() IApprove {
return &CEO{}
}
//HaveRight CEO总是有权限的
func (*CEO) HaveRight(RequiredLevel int) bool {
return true
}
//SetNext 设置下一个审批节点
func (c *CEO) SetNext(next IApprove) {
//no thing to do
}
//HandleApproval 进行审批
func (*CEO) HandleApproval(request FeeRequest) bool {
if request.Mount < 1e+15 {
fmt.Printf("CEO permit %s %d fee request\n", request.Name, request.Mount)
return true
}
fmt.Printf("CEO deny %s %d fee request \n", request.Name, request.Mount)
return false
}