• 设计模式--职责链模式


    使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系。将这个对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它为止。

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

    由于是在客户端来定义链的结构,也就是可以随时地增加或修改处理一个请求的结构。增强了给对象指派职责的灵活性。

    //申请
    class Request
    {
    //申请类别
    public string RequestType { get; set; }
    //申请内容
    public string RequestContent { get; set; }
    //数量
    public int Number { get; set; }
    }

    //定义管理者抽象类

    abstract class Manager
    {
    protected string name;
    //管理者的上级
    protected Manager supperior;

    public Manager(string name)
    {
    this.name = name;
    }
    //设置管理者的上级
    public void SetSuperlior(Manager manager)
    {
    this.supperior = manager;
    }
    //申请请求
    public abstract void RequestApplications(Request request);

    }

    //实现管理者类

    //经理
    class CommonManager : Manager
    {
    public CommonManager(string name) : base(name)
    {
    }

    public override void RequestApplications(Request request)
    {
    if(request.RequestType.Equals("请假")&&request.Number<=2)
    {
    Console.WriteLine("{0}:{1} 数量:{2}被批准", name, request.RequestContent, request.Number);
    }
    else
    {
    if(supperior!=null)
    {
    supperior.RequestApplications(request);
    }
    }

    }
    }

    //总监
    class Majordomo : Manager
    {
    public Majordomo(string name) : base(name)
    {
    }

    public override void RequestApplications(Request request)
    {
    if(request.RequestType.Equals("请假")&&request.Number<=5)
    {
    Console.WriteLine("{0}:{1} 数量:{2}被批准", name, request.RequestContent, request.Number);
    }
    else
    {
    if(supperior!=null)
    {
    supperior.RequestApplications(request);
    }
    }
    }
    }

    //总经理
    class GeneralManager : Manager
    {
    public GeneralManager(string name) : base(name)
    {
    }

    public override void RequestApplications(Request request)
    {
    if(request.RequestType.Equals("请假"))
    {
    Console.WriteLine("{0}:{1} 数量{2}被批准", name, request.RequestContent, request.Number);
    }
    else if(request.RequestType.Equals("加薪")&&request.Number<=5000)
    {
    Console.WriteLine("{0}:{1} 数量{2}被批准", name, request.RequestContent, request.Number);
    }
    else if(request.RequestType.Equals("加薪")&&request.Number>5000)
    {
    Console.WriteLine("{0}:{1} 数量{2}再说吧", name, request.RequestContent, request.Number);
    }
    }
    }

    调用:

    CommonManager jinli = new CommonManager("jinli");
    Majordomo zongjian = new Majordomo("zongjian");
    GeneralManager zhongjingli= new GeneralManager("zongjingli");

    //上级可以任意设置

    jinli.SetSuperlior(zongjian);
    zongjian.SetSuperlior(zhongjingli);

    Request req = new Request()
    {
    RequestType = "请假",
    RequestContent = "小菜请假",
    Number = 1
    };
    jinli.RequestApplications(req);

    req = new Request()
    {
    RequestType = "请假",
    RequestContent = "小菜请假",
    Number =10
    };
    jinli.RequestApplications(req);

    req = new Request()
    {
    RequestType = "请假",
    RequestContent = "小菜请假",
    Number = 3
    };
    jinli.RequestApplications(req);

    req = new Request()
    {
    RequestType = "加薪",
    RequestContent = "小菜请求加薪",
    Number = 5000
    };
    jinli.RequestApplications(req);

    req = new Request()
    {
    RequestType = "加薪",
    RequestContent = "小菜请求加薪",
    Number = 10000
    };
    jinli.RequestApplications(req);

  • 相关阅读:
    ECharts grid组件离容器的距离
    防火墙centos7执行 service iptables status报错问题完美解决
    linux 在切换用户时出现:命令提示符-bash-4.1$错误解决
    DataTable转为TXT文档
    读取ecxel中数据——NPOI.Excel和Aspose
    SQL连接数据库
    fiddler软件无法生成代码
    webapi发布后更新(无前台时)
    webapi日志记录(TXT存储)
    webapi使用Get进行访问时,url长度被限制解决办法
  • 原文地址:https://www.cnblogs.com/buzhidaojiaoshenme/p/6732132.html
Copyright © 2020-2023  润新知