Code Example |
1 // Chain Of Responsibility 2![](/Images/OutliningIndicators/None.gif) 3 // Intent: "Avoid coupling the sender of a request to its receiver by giving 4 // more than one object a chance to handle the reuqest. Chain the receiving 5 // objects and pass the request along the chain until an object handles it." 6![](/Images/OutliningIndicators/None.gif) 7 // For further information, read "Design Patterns", p223, Gamma et al., 8 // Addison-Wesley, ISBN:0-201-63361-2 9![](/Images/OutliningIndicators/None.gif) 10![](/Images/OutliningIndicators/ExpandedBlockStart.gif) /**//* Notes: 11 * The client sends a request and it will be operated on by one of a number 12 * of potential receivers, in a chain. The client does not know (and does 13 * not need to know) which receiver handles the request. The receivers are 14 * in a chain, and the request is passed from one to the next, until one 15 * receiver actually performs the request. 16 */ 17 18 namespace ChainOfResponsibility_DesignPattern 19![](/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](/Images/OutliningIndicators/ContractedBlock.gif) { 20 using System; 21![](/Images/OutliningIndicators/InBlock.gif) 22 abstract class Handler 23![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 24 protected Handler successorHandler; 25 abstract public void HandleRequest(Request request); 26 public void SetSuccessor(Handler sucessor) 27![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 28 successorHandler = sucessor; 29 } 30 } 31![](/Images/OutliningIndicators/InBlock.gif) 32 class ConcreteHandler1 : Handler 33![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 34 override public void HandleRequest(Request request) 35![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 36 // determine if we can handle the request 37 if (request.RequestType == 1) // some complex decision making! 38![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 39 // request handling code goes here 40 Console.WriteLine("request handled in ConcreteHandler1"); 41 } 42 else 43![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 44 // not handled here - pass on to next in the chain 45 if (successorHandler != null) 46 successorHandler.HandleRequest(request); 47 } 48 } 49 } 50![](/Images/OutliningIndicators/InBlock.gif) 51 class ConcreteHandler2 : Handler 52![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 53 override public void HandleRequest(Request request) 54![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 55 // determine if we can handle the request 56 if (request.RequestType == 2) // some complex decision making! 57![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 58 // request handling code goes here 59 Console.WriteLine("request handled in ConcreteHandler2"); 60 } 61 else 62![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 63 // not handled here - pass on to next in the chain 64 if (successorHandler != null) 65 successorHandler.HandleRequest(request); 66 } 67 } 68 } 69![](/Images/OutliningIndicators/InBlock.gif) 70 class ConcreteHandler3 : Handler 71![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 72 override public void HandleRequest(Request request) 73![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 74 // determine if we can handle the request 75 if (request.RequestType == 3) // some complex decision making! 76![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 77 // request handling code goes here 78 Console.WriteLine("request handled in ConcreteHandler3"); 79 } 80 else 81![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 82 // not handled here - pass on to next in the chain 83 if (successorHandler != null) 84 successorHandler.HandleRequest(request); 85 } 86 } 87 } 88![](/Images/OutliningIndicators/InBlock.gif) 89 class Request 90![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 91 private int iRequestType; 92 private string strRequestParameters; 93![](/Images/OutliningIndicators/InBlock.gif) 94 public Request(int requestType, string requestParameters) 95![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 96 iRequestType = requestType; 97 strRequestParameters = requestParameters; 98 } 99![](/Images/OutliningIndicators/InBlock.gif) 100 public int RequestType 101![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 102 get 103![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 104 return iRequestType; 105 } 106 set 107![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 108 iRequestType = value; 109 } 110 } 111 } 112![](/Images/OutliningIndicators/InBlock.gif) 113![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//// <summary> 114 /// Summary description for Client. 115 /// </summary> 116 public class Client 117![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 118 public static int Main(string[] args) 119![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 120 // Set up chain (usually one need to be done once) 121 Handler firstHandler = new ConcreteHandler1(); 122 Handler secondHandler = new ConcreteHandler2(); 123 Handler thirdHandler = new ConcreteHandler3(); 124 firstHandler.SetSuccessor(secondHandler); 125 secondHandler.SetSuccessor(thirdHandler); 126![](/Images/OutliningIndicators/InBlock.gif) 127 // After setting up the chain of responsibility, we can 128 // now generate requests and pass then off to the 129 // chain to be handled 130![](/Images/OutliningIndicators/InBlock.gif) 131 // generate and fire request 132 Request newRequest = new Request(2,"This are the request parameters"); 133 firstHandler.HandleRequest(newRequest); 134 135 return 0; 136 } 137 } 138 } 139![](/Images/OutliningIndicators/None.gif) 140![](/Images/OutliningIndicators/None.gif) |