设计模式——链接模式
名称 | Chain of Responsibility |
结构 | |
意图 | 使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系。将这些对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它为止。 |
适用性 |
|
Code Example |
1// Chain Of Responsibility 2 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 7// For further information, read "Design Patterns", p223, Gamma et al., 8// Addison-Wesley, ISBN:0-201-63361-2 9 10/* 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 18namespace ChainOfResponsibility_DesignPattern 19{ 20 using System; 21 22 abstract class Handler 23 { 24 protected Handler successorHandler; 25 abstract public void HandleRequest(Request request); 26 public void SetSuccessor(Handler sucessor) 27 { 28 successorHandler = sucessor; 29 } 30 } 31 32 class ConcreteHandler1 : Handler 33 { 34 override public void HandleRequest(Request request) 35 { 36 // determine if we can handle the request 37 if (request.RequestType == 1) // some complex decision making! 38 { 39 // request handling code goes here 40 Console.WriteLine("request handled in ConcreteHandler1"); 41 } 42 else 43 { 44 // not handled here - pass on to next in the chain 45 if (successorHandler != null) 46 successorHandler.HandleRequest(request); 47 } 48 } 49 } 50 51 class ConcreteHandler2 : Handler 52 { 53 override public void HandleRequest(Request request) 54 { 55 // determine if we can handle the request 56 if (request.RequestType == 2) // some complex decision making! 57 { 58 // request handling code goes here 59 Console.WriteLine("request handled in ConcreteHandler2"); 60 } 61 else 62 { 63 // not handled here - pass on to next in the chain 64 if (successorHandler != null) 65 successorHandler.HandleRequest(request); 66 } 67 } 68 } 69 70 class ConcreteHandler3 : Handler 71 { 72 override public void HandleRequest(Request request) 73 { 74 // determine if we can handle the request 75 if (request.RequestType == 3) // some complex decision making! 76 { 77 // request handling code goes here 78 Console.WriteLine("request handled in ConcreteHandler3"); 79 } 80 else 81 { 82 // not handled here - pass on to next in the chain 83 if (successorHandler != null) 84 successorHandler.HandleRequest(request); 85 } 86 } 87 } 88 89 class Request 90 { 91 private int iRequestType; 92 private string strRequestParameters; 93 94 public Request(int requestType, string requestParameters) 95 { 96 iRequestType = requestType; 97 strRequestParameters = requestParameters; 98 } 99 100 public int RequestType 101 { 102 get 103 { 104 return iRequestType; 105 } 106 set 107 { 108 iRequestType = value; 109 } 110 } 111 } 112 113 /// <summary> 114 /// Summary description for Client. 115 /// </summary> 116 public class Client 117 { 118 public static int Main(string[] args) 119 { 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 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 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 140 |