zoukankan      html  css  js  c++  java
  • 《大话设计模式》学习笔记20:职责链模式

      

      

    申请审批示例:

      

    1.请求类:

        public class Request
        {
            public string RequestType { get; set; }
            public string RequestContent { get; set; }
            public int Number { get; set; }
        }

    2.Handler:

        public abstract class Manager
        {
            protected string name;
            protected Manager superiorManager;
    
            public Manager(string name)
            {
                this.name = name;
            }
            public void SetSuperior(Manager superiorManager)
            {
                this.superiorManager = superiorManager;
            }
            public abstract void RequestApplication(Request request);
        }

    3.ConcreteHandler:

        public class CommonManager:Manager
        {
            public CommonManager(string name)
                :base(name)
            {
    
            }
            public override void RequestApplication(Request request)
            {
                if(request.RequestType=="请假"&&request.Number<=2)
                {
                    Console.WriteLine("{0}:{1} 数量{2} 被批准", name, request.RequestType, request.Number);
                }
                else
                {
                    if(superiorManager!=null)
                    {
                        superiorManager.RequestApplication(request);
                    }
                }
            }
        }
        public class Majordomo:Manager
        {
            public Majordomo(string name)
                :base(name)
            {
    
            }
            public override void RequestApplication(Request request)
            {
                if(request.RequestType=="请假"&&request.Number<=5)
                {
                    Console.WriteLine("{0}:{1} 数量{2} 被批准", name, request.RequestType, request.Number);
                }
                else
                {
                    if(superiorManager!=null)
                    {
                        superiorManager.RequestApplication(request);
                    }
                }
            }
        }
        public class GeneralManager:Manager
        {
            public GeneralManager(string name)
                :base(name)
            {
    
            }
            public override void RequestApplication(Request request)
            {
                if(request.RequestType=="请假")
                {
                    Console.WriteLine("{0}:{1} 数量{2} 被批准", name, request.RequestType, request.Number);
                }
                else if(request.RequestType=="加薪")
                {
                    if (request.Number <= 500)
                    {
                        Console.WriteLine("{0}:{1} 数量{2} 被批准", name, request.RequestType, request.Number);
                    }
                    else
                    {
                        Console.WriteLine("{0}:{1} 数量{2} 再说吧", name, request.RequestType, request.Number);
                    }
                }
            }
        }

    客户端代码:

        class Program
        {
            static void Main(string[] args)
            {
                Manager commonManager = new CommonManager("经理");
                Manager majordom = new Majordomo("总监");
                Manager generalManager = new GeneralManager("总经理");
    
                commonManager.SetSuperior(majordom);
                majordom.SetSuperior(generalManager);
    
                Request request = new Request();
    
                request.RequestType = "请假"; request.RequestContent = "事假"; request.Number = 1;
                commonManager.RequestApplication(request);
    
                request.RequestType = "请假"; request.RequestContent = "年假"; request.Number = 4;
                commonManager.RequestApplication(request);
    
                request.RequestType = "加薪"; request.RequestContent = "加薪一千"; request.Number = 1000;
                commonManager.RequestApplication(request);
            }
        }

      优点:接收者和发送者都没有对方的明确信息,且链中对象自己也并不知道链的结构。结果是职责链可简化对象的相互连接,它们仅需保持一个指向其后继者的引用,而不需要保持它所有候选接受者的引用。

  • 相关阅读:
    Robot Framework-取出接口返回的字段
    利用VBA宏批量解决Word中图片大小、居中设置
    封装和继承
    Shiro的学习
    Redis集群搭建
    Mysql主从架构
    idea修改maven的默认配置不生效
    Mybatis笔记
    【python】文件的读写方法(r;r+;rb;w;w+;wb;a;a+;ab)
    【Python】Xpath语法&示例
  • 原文地址:https://www.cnblogs.com/walden1024/p/4528972.html
Copyright © 2011-2022 走看看