首先我们要写一个请求的类。
class Request { private int day; private string reason; public int Day { get { return day; } set { day = value; } } public string Reason { get { return reason; } set { reason = value; } } public Request(int day, string reason) { this.day = day; this.reason = reason; } }
接下来看下请求相应者,他们有两个核心方法,一个是相应操作,一个是选择继任者。
abstract class Boss { private string name; public string Name { get { return name; } set { name = value; } } private Boss successor; public Boss Successor { get { return successor; } set { successor = value; } } public Boss(string name) { this.name = name; } public abstract bool PassRequest(Request request); } class PM:Boss { public PM(string name) : base(name) { } public override bool PassRequest(Request request) { int day = request.Day; string reason = request.Reason; if (day <= 0.5) { return true; } return Successor.PassRequest(request); } } class HR:Boss { public HR(string name) : base(name) { } public override bool PassRequest(Request request) { int day = request.Day; string reason = request.Reason; if (day > 0.5&&day<=2) { return true; } return Successor.PassRequest(request); } } class Manager : Boss { public Manager(string name) : base(name) { } public override bool PassRequest(Request request) { int day = request.Day; string reason = request.Reason; if (reason.Equals("正当理由")) { return true; } return false; } }
那么我们调用的时候就很简单了!
static void Main(string[] args) { Request request = new Request(3, "非正当理由"); Boss pm = new PM("pm"); Boss hr = new HR("hr"); Boss manager = new Manager("manager"); pm.Successor = hr; hr.Successor = manager; bool pass = pm.PassRequest(request); Console.Write(pass); }