Design Pattern_Skill_5_責任鏈模式(Chain of Responsibility Pattern)_經費申請加簽邏輯實踐

在公司當中通常
都會跑一些申請簽核單、跑公文
要申請經費之類的

可能會跑類似表單簽核的
這裡就以經費大小來模擬介紹一次CRP

Hander:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ResponsibilityChainPattern
{
    public abstract class Hander
    {
        public Hander successor;
        public void SetSuccessor(Hander successor)
        {
            this.successor = successor;
        }

        public abstract void HandleRequest(CostAndBudget costAndBudget);
    }
}


CostAndBudget:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ResponsibilityChainPattern
{
    public class CostAndBudget
    {
        private string name;
        private int amount;

        public CostAndBudget(string name, int amount)
        {
            this.name = name;
            this.amount = amount;
        }

        public string Name { get => name; }
        public int Amount { get => amount; }
    }
}


Manager , Director , CEO 三個角色:

 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ResponsibilityChainPattern
{
    /// <summary>
    /// CEO
    /// </summary>
    class CEO : Hander
    {
        public override void HandleRequest(CostAndBudget costAndBudget)
        {
            Console.WriteLine(String.Format("{0} ->經費8萬以上 , 由CEO審核", costAndBudget.Name));
        }
    }

    /// <summary>
    /// 處長
    /// </summary>
    class Director : Hander
    {
        public Director()
        {
            SetSuccessor(new CEO());
        }

        public override void HandleRequest(CostAndBudget costAndBudget)
        {
            if(costAndBudget.Amount > 80000)
            {
                successor.HandleRequest(costAndBudget);
            }
            else
            {
                Console.WriteLine(String.Format("{0} ->經費4萬以上至8萬 , 由處長審核", costAndBudget.Name));
            }            
        }
    }

    /// <summary>
    /// 經理
    /// </summary>
    class Manager : Hander
    {
        public Manager()
        {
            SetSuccessor(new Director());
        }

        public override void HandleRequest(CostAndBudget costAndBudget)
        {
            if (costAndBudget.Amount > 40000)
            {
                successor.HandleRequest(costAndBudget);
            }
            else
            {
                Console.WriteLine(String.Format("{0} ->經費4萬以內 , 由經理審核", costAndBudget.Name));
            }
        }
    }

}



主程式呼叫端:

 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ResponsibilityChainPattern
{
    class Program
    {
        static void Main(string[] args)
        {
            Hander manager = new Manager();//指定第一關(職位階層)
            manager.HandleRequest(new CostAndBudget("出差交通費報銷2千元", 2000));
            manager.HandleRequest(new CostAndBudget("宣傳費5萬", 50000));
            manager.HandleRequest(new CostAndBudget("買一台50萬的機台", 500000));

            //CostAndBudget smallCost = new CostAndBudget("出差交通費報銷2千元", 2000);
            //manager.HandleRequest(smallCost);
            //CostAndBudget middleCost = new CostAndBudget("宣傳費5萬", 50000);
            //manager.HandleRequest(middleCost);
            //CostAndBudget bigCost = new CostAndBudget("買一台50萬的機台",500000);
            //manager.HandleRequest(bigCost);

            Console.ReadKey();
        }
    }
}







OK那在此模式上可以看到往後
我不需要去透過if  ... else if ....來實作一個簽核下至上流程
而此模式多數用於類似加簽這類業務邏輯






留言

這個網誌中的熱門文章

何謂淨重(Net Weight)、皮重(Tare Weight)與毛重(Gross Weight)

Architecture(架構) 和 Framework(框架) 有何不同?_軟體設計前的事前規劃的藍圖概念

(2021年度)駕訓學科筆試準備題庫歸納分析_法規是非題