// 具体请求处理方法 public override void ProcessRequest(PurchaseRequest request) { if (request.Amount < 100000) { // 处理请求 Console.WriteLine("副总裁 {0} 审批采购单:{1},金额:{2} 元,采购目的:{3}。", this.name, request.Number, request.Amount, request.Purpose); } else { // 如果处理不了,转发请求给更高层领导 this.successor.ProcessRequest(request); } }
public static void Main(string[] args) { // 创建职责链 Approver andy = new Director("Andy"); Approver jacky = new VicePresident("Jacky"); Approver ashin = new President("Ashin"); Approver meeting = new Congress("Congress"); andy.SetSuccessor(jacky); jacky.SetSuccessor(ashin); ashin.SetSuccessor(meeting); // 构造采购请求单并发送审批请求 PurchaseRequest request1 = new PurchaseRequest(45000.00, "MANULIFE201706001", "购买PC和显示器"); andy.ProcessRequest(request1); PurchaseRequest request2 = new PurchaseRequest(60000.00, "MANULIFE201706002", "2017开发团队活动"); andy.ProcessRequest(request2); PurchaseRequest request3 = new PurchaseRequest(160000.00, "MANULIFE201706003", "2017公司年度旅游"); andy.ProcessRequest(request3); PurchaseRequest request4 = new PurchaseRequest(800000.00, "MANULIFE201706004", "租用新临时办公楼"); andy.ProcessRequest(request4); Console.ReadKey(); }
// 抽象请求处理方法 public abstract void ProcessRequest(PurchaseRequest request);
// 具体请求处理方法 public override void ProcessRequest(PurchaseRequest request) { // 处理请求 Console.WriteLine("董事会 {0} 审批采购单:{1},金额:{2} 元,采购目的:{3}。", this.name, request.Number, request.Amount, request.Purpose); }