[設計模式練習]職責鍊模式

[設計模式練習]職責鍊模式

將請假過程所要簽核的主管利用職責鍊模式設計

簽核順序為 經理 總監 總經理

類別圖

ChainOfResponsibility

物件檔定義


using System.Collections.Generic;
using System.Text;

namespace DesignModelTest.職責鍊
{
    /// <summary>
    /// Handler類別 : 管理階層
    /// </summary>
    abstract class Manager
    {
        protected string name;
        protected Manager superior;
        public Manager(string name)
        {
            this.name = name;
        }
        public void SetSuperior(Manager superior)
        {
            this.superior = superior;
        }
        //傳遞訊息
        abstract public void RequestApplication(Request request);
    }
    /// <summary>
    /// 要求
    /// </summary>
    class Request
    {
        public string RequestContent;//內容
        public string RequestType;//事由
        public int Number;//旗標
    }
    /// <summary>
    /// ConcreteHandler類別 : 經理
    /// </summary>
    class CommonManager : Manager
    {
        public CommonManager(string name) : base(name) { }
        public override void RequestApplication(Request request)
        {
            //當符合條件就處理(request.Number <= 2)否則傳給下一個ConcreteHandler(總監)
            if (request.Number <= 2)
            {
                Console.WriteLine(name + "\t核準 " + request.RequestContent + "\t" + request.RequestType + "\t" + request.Number.ToString() );
            }
            else
            {
                if (superior != null)
                    superior.RequestApplication(request);
            }
        }
    }
    /// <summary>
    /// ConcreteHandler類別 : 總監
    /// </summary>
    class Majordomo : Manager
    {
        public Majordomo(string name) : base(name) { }
        public override void RequestApplication(Request request)
        {
            //當符合條件就處理(request.Number <= 5)否則傳給下一個ConcreteHandler(總經理)
            if (request.Number <= 5)
            {
                Console.WriteLine(name + "\t核準 " + request.RequestContent + "\t" + request.RequestType + "\t" + request.Number.ToString() );
            }
            else
            {
                if (superior != null)
                    superior.RequestApplication(request);
            }
        }
    }
    /// <summary>
    /// ConcreteHandler類別 : 總經理
    /// </summary>
    class GeneralManager : Manager
    {
        public GeneralManager(string name) : base(name) { }
        public override void RequestApplication(Request request)
        {
            //當符合條件就處理(request.Number <= 10)就核準,否則不核準
            if (request.Number <= 10)
            {
                Console.WriteLine(name +"\t核準 "+request.RequestContent + "\t" + request.RequestType + "\t" + request.Number.ToString() );
            }
            else
            {
                Console.WriteLine(name + "\t不核準 " + request.RequestContent + "\t" + request.RequestType + "\t" + request.Number.ToString());
            }
        }
    }
}

用戶端程式碼


            DesignModelTest.職責鍊.CommonManager cm = new DesignModelTest.職責鍊.CommonManager("王經理");
            DesignModelTest.職責鍊.Majordomo md = new DesignModelTest.職責鍊.Majordomo("李總監");
            DesignModelTest.職責鍊.GeneralManager gm = new DesignModelTest.職責鍊.GeneralManager("林總經理");
            //設定職責鍊的上下家
            cm.SetSuperior(md);
            md.SetSuperior(gm);
            DesignModelTest.職責鍊.Request request1 = new DesignModelTest.職責鍊.Request();
            request1.Number = 1;
            request1.RequestContent = "小廖請假";
            request1.RequestType = "婚假";
            cm.RequestApplication(request1);
            request1.Number = 3;
            cm.RequestApplication(request1);
            request1.Number = 7;
            cm.RequestApplication(request1);
            request1.Number = 11;
            cm.RequestApplication(request1);
            Console.Read();
            #endregion

輸出結果

pic2