重要概念
1. 使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系。将这个对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它为止。
2.请求是沿着链条传递到有一个处理点对象负责处理这个请求为止。请求者无需关心谁会处理这个请求。
3. 接收者和发送者都没有对方的明确信息,且链中的对象自己也并不知道链的结构。结果是职责链可简化对象的相互连接,他们仅需保持一个指向其后继者的引用,而不需要保持它所有的候选接收者的引用。
4. 从执行流程上感觉和 case break 相似。
5. 随时地增加或修改该处理一个请求的结构(处理顺序,处理点数量),增强了给对象指派职责的灵活性。
6. 解决大量分支判断造成的难维护,灵活性差的问题。
示例程序
using System; using System.Collections.Generic; using System.Text; namespace 职责链模式 { class Program { static void Main(string[] args) { //初始化三个职责链处理点 Handler h1 = new ConcreteHandler1(); Handler h2 = new ConcreteHandler2(); Handler h3 = new ConcreteHandler3(); //设置职责链的连接处理顺序 h1.SetSuccessor(h2); h2.SetSuccessor(h3); int[] requests = { 2, 5, 14, 22, 18, 3, 27, 20 }; foreach (int request in requests) { //要从职责链的最小端,也就是开始的那头发出请求。 h1.HandleRequest(request); } Console.Read(); } } abstract class Handler { protected Handler successor; public void SetSuccessor(Handler successor) { this.successor = successor; } public abstract void HandleRequest(int request); } class ConcreteHandler1 : Handler { public override void HandleRequest(int request) { if (request >= 0 && request < 10) { Console.WriteLine("{0} 处理请求 {1}", this.GetType().Name, request); } else if (successor != null) { successor.HandleRequest(request); } } } class ConcreteHandler2 : Handler { public override void HandleRequest(int request) { if (request >= 10 && request < 20) { Console.WriteLine("{0} 处理请求 {1}", this.GetType().Name, request); } else if (successor != null) { successor.HandleRequest(request); } } } class ConcreteHandler3 : Handler { public override void HandleRequest(int request) { if (request >= 20 && request < 30) { Console.WriteLine("{0} 处理请求 {1}", this.GetType().Name, request); } else if (successor != null) { successor.HandleRequest(request); } } } }
加薪代码2
using System; using System.Collections.Generic; using System.Text; namespace 职责链模式 { class Program { static void Main(string[] args) { CommonManager jinli = new CommonManager("金利"); Majordomo zongjian = new Majordomo("宗剑"); GeneralManager zhongjingli = new GeneralManager("钟精励"); jinli.SetSuperior(zongjian); zongjian.SetSuperior(zhongjingli); Request request = new Request(); request.RequestType = "请假"; request.RequestContent = "小菜请假"; request.Number = 1; jinli.RequestApplications(request); Request request2 = new Request(); request2.RequestType = "请假"; request2.RequestContent = "小菜请假"; request2.Number = 4; jinli.RequestApplications(request2); Request request3 = new Request(); request3.RequestType = "加薪"; request3.RequestContent = "小菜请求加薪"; request3.Number = 500; jinli.RequestApplications(request3); Request request4 = new Request(); request4.RequestType = "加薪"; request4.RequestContent = "小菜请求加薪"; request4.Number = 1000; jinli.RequestApplications(request4); Console.Read(); } } //管理者 abstract class Manager { protected string name; //管理者的上级 protected Manager superior; public Manager(string name) { this.name = name; } //设置管理者的上级 public void SetSuperior(Manager superior) { this.superior = superior; } //申请请求 abstract public void RequestApplications(Request request); } //经理 class CommonManager : Manager { public CommonManager(string name) : base(name) { } public override void RequestApplications(Request request) { if (request.RequestType == "请假" && request.Number <= 2) { Console.WriteLine("{0}:{1} 数量{2} 被批准", name, request.RequestContent, request.Number); } else { if (superior != null) superior.RequestApplications(request); } } } //总监 class Majordomo : Manager { public Majordomo(string name) : base(name) { } public override void RequestApplications(Request request) { if (request.RequestType == "请假" && request.Number <= 5) { Console.WriteLine("{0}:{1} 数量{2} 被批准", name, request.RequestContent, request.Number); } else { if (superior != null) superior.RequestApplications(request); } } } //总经理 class GeneralManager : Manager { public GeneralManager(string name) : base(name) { } public override void RequestApplications(Request request) { if (request.RequestType == "请假") { Console.WriteLine("{0}:{1} 数量{2} 被批准", name, request.RequestContent, request.Number); } else if (request.RequestType == "加薪" && request.Number <= 500) { Console.WriteLine("{0}:{1} 数量{2} 被批准", name, request.RequestContent, request.Number); } else if (request.RequestType == "加薪" && request.Number > 500) { Console.WriteLine("{0}:{1} 数量{2} 再说吧", name, request.RequestContent, request.Number); } } } //申请 class Request { //申请类别 private string requestType; public string RequestType { get { return requestType; } set { requestType = value; } } //申请内容 private string requestContent; public string RequestContent { get { return requestContent; } set { requestContent = value; } } //数量 private int number; public int Number { get { return number; } set { number = value; } } } }
冯瑞涛