finish chain_of_responsibility pattern

This commit is contained in:
Edward 2020-05-04 01:22:30 +08:00
parent d08b2bd23c
commit 0a186384f9
2 changed files with 34 additions and 14 deletions

View File

@ -67,8 +67,8 @@ func (g *GM) SetNext(next IApprove) {
} }
//HaveRight 处理审批所需要的权限级别 //HaveRight 处理审批所需要的权限级别
func (g *GM) HaveRight(level int) bool { func (g *GM) HaveRight(RequiredLevel int) bool {
return g.level > level return g.level > RequiredLevel
} }
//HandleApproval 进行审批 //HandleApproval 进行审批
@ -77,7 +77,7 @@ func (g *GM) HandleApproval(request FeeRequest) bool {
fmt.Printf("GM permit %s %d fee request\n", request.Name, request.Mount) fmt.Printf("GM permit %s %d fee request\n", request.Name, request.Mount)
return true return true
} }
fmt.Printf("GM Have right to approve %s %d fee request\n", request.Name, request.Mount) fmt.Printf("GM NO right to approve %s %d fee request\n", request.Name, request.Mount)
//direct forward to Next One //direct forward to Next One
if g.nextHandler != nil { if g.nextHandler != nil {
return g.nextHandler.HandleApproval(request) return g.nextHandler.HandleApproval(request)
@ -113,7 +113,7 @@ func (c *CFO) HandleApproval(request FeeRequest) bool {
fmt.Printf("CFO permit %s %d fee request\n", request.Name, request.Mount) fmt.Printf("CFO permit %s %d fee request\n", request.Name, request.Mount)
return true return true
} }
// fmt.Printf("CFO permit %s %d fee request,But still need CEO \n", request.Name, request.Mount) fmt.Printf("CFO No right to approve %s %d fee request \n", request.Name, request.Mount)
if c.nextHandler != nil { if c.nextHandler != nil {
return c.nextHandler.HandleApproval(request) return c.nextHandler.HandleApproval(request)
} }
@ -121,7 +121,8 @@ func (c *CFO) HandleApproval(request FeeRequest) bool {
} }
//CEO 需要审批 //CEO 需要审批
type CEO struct{} type CEO struct {
}
//NewCEO 对象 //NewCEO 对象
func NewCEO() IApprove { func NewCEO() IApprove {

View File

@ -6,7 +6,7 @@ func TestChainsOfResponsibilityInApprovementList(t *testing.T) {
request := FeeRequest{ request := FeeRequest{
Name: "sophia", Name: "sophia",
Mount: 1e+13, Mount: 1e+9,
RequiredLevel: 8, RequiredLevel: 8,
} }
@ -21,19 +21,38 @@ func TestChainsOfResponsibilityInApprovementList(t *testing.T) {
flow.RunApprovalFlow(request) flow.RunApprovalFlow(request)
// request = FeeRequest{ t.Log("------second flow--------")
// Name: "peter", request = FeeRequest{
// Mount: 1e+13, Name: "peter",
// RequiredLevel: 8, Mount: 1e+13,
// } RequiredLevel: 8,
}
// ceo := &CEO{level: 10} ceo := &CEO{}
// flow.AddApprover(ceo) flow.AddApprover(ceo)
// flow.RunApprovalFlow(request) flow.RunApprovalFlow(request)
} }
func TestChainsOfResponsibilityInApprovementLink(t *testing.T) { func TestChainsOfResponsibilityInApprovementLink(t *testing.T) {
request := FeeRequest{
Name: "sophia",
Mount: 1e+11,
RequiredLevel: 8,
}
gm := &GM{level: 7}
cfo := &CFO{level: 9}
ceo := &CEO{}
gm.SetNext(cfo)
cfo.SetNext(ceo)
gm.HandleApproval(request)
} }