设计模式学习笔记--职责链模式
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using System; 2 3 namespace ChainOfResponsibility 4 { 5 /// <summary> 6 /// 作者:bzyzhang 7 /// 时间:2016/6/1 6:54:00 8 /// 博客地址:http://www.cnblogs.com/bzyzhang/ 9 /// Handler说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 10 /// </summary> 11 public abstract class Handler 12 { 13 protected Handler successor; 14 15 public void SetSuccessor(Handler successor) 16 { 17 this.successor = successor; 18 } 19 20 public abstract void HandleRequest(int request); 21 } 22 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using System; 2 3 namespace ChainOfResponsibility 4 { 5 /// <summary> 6 /// 作者:bzyzhang 7 /// 时间:2016/6/1 6:56:18 8 /// 博客地址:http://www.cnblogs.com/bzyzhang/ 9 /// ConcreteHandler1说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 10 /// </summary> 11 public class ConcreteHandler1:Handler 12 { 13 public override void HandleRequest(int request) 14 { 15 if (request >= 0 && request < 10) 16 { 17 Console.WriteLine("{0}处理请求{1}",this.GetType().Name,request); 18 } 19 else if(successor != null) 20 { 21 successor.HandleRequest(request); 22 } 23 } 24 } 25 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using System; 2 3 namespace ChainOfResponsibility 4 { 5 /// <summary> 6 /// 作者:bzyzhang 7 /// 时间:2016/6/1 6:59:24 8 /// 博客地址:http://www.cnblogs.com/bzyzhang/ 9 /// ConcreteHandler2说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 10 /// </summary> 11 public class ConcreteHandler2:Handler 12 { 13 public override void HandleRequest(int request) 14 { 15 if (request >= 10 && request < 20) 16 { 17 Console.WriteLine("{0}处理请求{1}", this.GetType().Name, request); 18 } 19 else if (successor != null) 20 { 21 successor.HandleRequest(request); 22 } 23 } 24 } 25 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using System; 2 3 namespace ChainOfResponsibility 4 { 5 /// <summary> 6 /// 作者:bzyzhang 7 /// 时间:2016/6/1 7:00:21 8 /// 博客地址:http://www.cnblogs.com/bzyzhang/ 9 /// ConcreteHandler3说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 10 /// </summary> 11 public class ConcreteHandler3:Handler 12 { 13 public override void HandleRequest(int request) 14 { 15 if (request >= 20 && request < 30) 16 { 17 Console.WriteLine("{0}处理请求{1}", this.GetType().Name, request); 18 } 19 else if (successor != null) 20 { 21 successor.HandleRequest(request); 22 } 23 } 24 } 25 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace ChainOfResponsibility 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 Handler h1 = new ConcreteHandler1(); 14 Handler h2 = new ConcreteHandler2(); 15 Handler h3 = new ConcreteHandler3(); 16 h1.SetSuccessor(h2); 17 h2.SetSuccessor(h3); 18 19 int[] requests = { 2,5,14,22,18,3,27,20}; 20 21 foreach (int request in requests) 22 { 23 h1.HandleRequest(request); 24 } 25 } 26 } 27 }
随意耕种,只为自己!