From 0a186384f9c027855abbce7bf121fd53cb46a7fc Mon Sep 17 00:00:00 2001 From: Edward Date: Mon, 4 May 2020 01:22:30 +0800 Subject: [PATCH] finish chain_of_responsibility pattern --- .../responsibility_chain.go | 11 +++--- .../responsibility_chain_test.go | 37 ++++++++++++++----- 2 files changed, 34 insertions(+), 14 deletions(-) diff --git a/behavior/06_chain_of_responsibility/responsibility_chain.go b/behavior/06_chain_of_responsibility/responsibility_chain.go index 90ec23c..05ad01d 100644 --- a/behavior/06_chain_of_responsibility/responsibility_chain.go +++ b/behavior/06_chain_of_responsibility/responsibility_chain.go @@ -67,8 +67,8 @@ func (g *GM) SetNext(next IApprove) { } //HaveRight 处理审批所需要的权限级别 -func (g *GM) HaveRight(level int) bool { - return g.level > level +func (g *GM) HaveRight(RequiredLevel int) bool { + return g.level > RequiredLevel } //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) 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 if g.nextHandler != nil { 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) 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 { return c.nextHandler.HandleApproval(request) } @@ -121,7 +121,8 @@ func (c *CFO) HandleApproval(request FeeRequest) bool { } //CEO 需要审批 -type CEO struct{} +type CEO struct { +} //NewCEO 对象 func NewCEO() IApprove { diff --git a/behavior/06_chain_of_responsibility/responsibility_chain_test.go b/behavior/06_chain_of_responsibility/responsibility_chain_test.go index cc3b841..e52ac49 100644 --- a/behavior/06_chain_of_responsibility/responsibility_chain_test.go +++ b/behavior/06_chain_of_responsibility/responsibility_chain_test.go @@ -6,7 +6,7 @@ func TestChainsOfResponsibilityInApprovementList(t *testing.T) { request := FeeRequest{ Name: "sophia", - Mount: 1e+13, + Mount: 1e+9, RequiredLevel: 8, } @@ -21,19 +21,38 @@ func TestChainsOfResponsibilityInApprovementList(t *testing.T) { flow.RunApprovalFlow(request) - // request = FeeRequest{ - // Name: "peter", - // Mount: 1e+13, - // RequiredLevel: 8, - // } + t.Log("------second flow--------") + request = FeeRequest{ + Name: "peter", + Mount: 1e+13, + RequiredLevel: 8, + } - // ceo := &CEO{level: 10} - // flow.AddApprover(ceo) + ceo := &CEO{} + flow.AddApprover(ceo) - // flow.RunApprovalFlow(request) + flow.RunApprovalFlow(request) } 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) + }