代码:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { /// <summary> /// 请求类 场景 /// </summary> class Context { public Context(int day) { this.day = day; } private int day; public int Day { get { return day; } set { day = value; } } } /// <summary> /// 抽象基类 /// </summary> abstract class Handler { public Handler(string name) { _name = name; } private string _name; public string Name { get { return _name; } set { _name = value; } } private Handler _handler; public Handler handler { get { return _handler; } set { _handler = value; } } //是否通过 public abstract bool Pass(Context context); } /// <summary> /// 部门经理 2天以下的部门签字就成了 /// /// </summary> class Manager : Handler { public Manager(string name) : base(name) { } public override bool Pass(Context context) { if (context.Day <= 2) { Console.Write("{0}已经签字通过\n", Name); return true; } return handler.Pass(context); } } /// <summary> /// 总经理 2天以上需要总经理签字 /// </summary> class GeneralManager : Handler { public GeneralManager(string name) : base(name) { } public override bool Pass(Context context) { if (2 < context.Day && context.Day<=7) { Console.Write("{0}已经签字通过\n", Name); return true; } return handler.Pass(context); } } /// <summary> /// 董事长 7天以上需要总经理签字 /// </summary> class President : Handler { public President(string name) : base(name) { } public override bool Pass(Context context) { if (context.Day > 7) { Console.Write("{0}已经签字通过\n", Name); return true; } return handler.Pass(context); } } class Program { static void Main(string[] args) { Context context1 = new Context(1); Context context2 = new Context(5); Context context3 = new Context(100); Handler manager = new Manager("部门经理"); Handler gmanager = new GeneralManager("总经理"); Handler president = new President("董事长"); manager.handler = gmanager; gmanager.handler = president; manager.Pass(context1); manager.Pass(context2); manager.Pass(context3); Console.ReadLine(); } } }