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; } }
主代码:
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);