• 责任链模式 与中间件


    1。 先看责任链模式:请假流程,抽象的经理和具体的级别不同的经理(只贴出级别1的经理)。

        public abstract class Manager
        {
            public string Name { get; set; }
            public Manager Leader { get; set; }
            public abstract void Approve(Request request);
        }
    public class ManagerLevel1: Manager
        {
            public int Day { get; set; } = 3;
            public override void Approve(Request request)
            {
                if (request.Day <= Day)
                {
                    Console.WriteLine($"一级管理者 {Name} 批准了 {request.Name},{request.Day}天");
                }
                else
                {
                    Console.WriteLine($"{Name} 交给leader, {request.Day}天");
                    Leader.Approve(request);
                }
            }
        }
        public class Request
        {
            public int Day { get; set; }
            public string Name { get; set; }
        }
    View Code

    主代码:

     Console.WriteLine("Hello World!");
    
                var app = new App();
    
                var m1 = new ManagerLevel1();
                var m2 = new ManagerLevel2();
                var m3 = new ManagerLevel3();
    
                m1.Leader = m2;
                m2.Leader = m3;
    
                var re = new Request() { Name = "张三", Day = 6};
                m1.Approve(re);

    责任链模式结束,增加管理者:

        public  class App
        {
            List<Manager> managers { get; set; } = new List<Manager>();
    
            public App Use(Manager m)
            {
                var last= managers.LastOrDefault();
                if (last != null)
                    last.Leader = m;
    
                managers.Add(m);
    
                return this;
            }
        }

    主代码变成这样:

    Console.WriteLine("Hello World!");
    
    var app = new App();
    
    var m1 = new ManagerLevel1();
    var m2 = new ManagerLevel2();
    var m3 = new ManagerLevel3();
    
    app.Use(m1);
    app.Use(m2);
    app.Use(m3);
    
    var re = new Request() { Name = "张三", Day = 6};
    m1.Approve(re);
    气功波(18037675651)
  • 相关阅读:
    SPA项目开发之登录
    使用vue-cli搭建SPA项目
    ElementUI入门和NodeJS环境搭建
    struts文件上传
    Struts增删改查
    struts
    Maven
    easyui三
    EasyUi权限
    自定义MVC三
  • 原文地址:https://www.cnblogs.com/qgbo/p/14495203.html
Copyright © 2020-2023  润新知