• 责任链模式


    
    
    namespace ChainofResponsibility
    {
        // 采购请求
        public class PurchaseRequest
        {
            // 金额
            public double Amount { get; set; }
            // 产品名字
            public string ProductName { get; set; }
            public PurchaseRequest(double amount, string productName)
            {
                Amount = amount;
                ProductName = productName;
            }
        }
    
        // 审批人,Handler
        public abstract class Approver
        {
            public Approver NextApprover { get; set; }
            public string Name { get; set; }
            public Approver(string name)
            {
                this.Name = name;
            }
            public abstract void ProcessRequest(PurchaseRequest request);
        }
    
        // ConcreteHandler
        public class Manager : Approver
        {
            public Manager(string name)
                : base(name)
            { }
    
            public override void ProcessRequest(PurchaseRequest request)
            {
                if (request.Amount < 10000.0)
                {
                    Console.WriteLine("{0}-{1} approved the request of purshing {2}", this, Name, request.ProductName);
                }
                else if (NextApprover != null)
                {
                    NextApprover.ProcessRequest(request);
                }
            }
        }
    
        // ConcreteHandler,副总
        public class VicePresident : Approver
        {
            public VicePresident(string name)
                : base(name)
            { 
            }
            public override void ProcessRequest(PurchaseRequest request)
            {
                if (request.Amount < 25000.0)
                {
                    Console.WriteLine("{0}-{1} approved the request of purshing {2}", this, Name, request.ProductName);
                }
                else if (NextApprover != null)
                {
                    NextApprover.ProcessRequest(request);
                }
            }
        }
    
        // ConcreteHandler,总经理
        public class President :Approver
        {
            public President(string name)
                : base(name)
            { }
            public override void ProcessRequest(PurchaseRequest request)
            {
                if (request.Amount < 100000.0)
                {
                    Console.WriteLine("{0}-{1} approved the request of purshing {2}", this, Name, request.ProductName);
                }
                else
                {
                    Console.WriteLine("Request需要组织一个会议讨论");
                }
            }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                PurchaseRequest requestTelphone = new PurchaseRequest(4000.0, "Telphone");
                PurchaseRequest requestSoftware = new PurchaseRequest(10000.0, "Visual Studio");
                PurchaseRequest requestComputers = new PurchaseRequest(40000.0, "Computers");
    
                Approver manager = new Manager("LearningHard");
                Approver Vp = new VicePresident("Tony");
                Approver Pre = new President("BossTom");
    
                // 设置责任链
                manager.NextApprover = Vp;
                Vp.NextApprover = Pre;
    
                // 处理请求
                manager.ProcessRequest(requestTelphone);
                manager.ProcessRequest(requestSoftware);
                manager.ProcessRequest(requestComputers);
                Console.ReadLine();
            }
        }
    }
    
    
    
     
  • 相关阅读:
    2017-2018-2 20165207 实验四《Android开发基础》实验报告
    2017-2018-2 20165207 实验三《敏捷开发与XP实践》实验报告
    20165207 第九周学习总结
    20165328 实验四《Andriid应用开发》实验报告
    20165328 第十二周课上补做
    20165328 课下作业补做
    20165328 第九周学习总结
    2017-2018-2 20165328 实验三《敏捷开发与XP实践》实验报告
    20165328 结对编程第二周整体总结
    20165328课上补做
  • 原文地址:https://www.cnblogs.com/gaocong/p/6924705.html
Copyright © 2020-2023  润新知