设计模式 职责连模式(Chain of Responsibility)
意图
使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系。将这些对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它为止。
适用性
1.有多个的对象可以处理一个请求,哪个对象处理该请求运行时刻自动确定。
2.你想在不明确指定接收者的情况下,向多个对象中的一个提交一个请求。
3.可处理一个请求的对象集合应被动态指定。
结构图
Code
1 // Chain Of Responsibility
2
3 /* Notes:
4 * The client sends a request and it will be operated on by one of a number
5 * of potential receivers, in a chain. The client does not know (and does
6 * not need to know) which receiver handles the request. The receivers are
7 * in a chain, and the request is passed from one to the next, until one
8 * receiver actually performs the request.
9 */
10
11 namespace ChainOfResponsibility_DesignPattern
12 {
13 using System;
14
15 abstract class Handler
16 {
17 protected Handler successorHandler;
18 abstract public void HandleRequest(Request request);
19 public void SetSuccessor(Handler sucessor)
20 {
21 successorHandler = sucessor;
22 }
23 }
24
25 class ConcreteHandler1 : Handler
26 {
27 override public void HandleRequest(Request request)
28 {
29 // determine if we can handle the request
30 if (request.RequestType == 1) // some complex decision making!
31 {
32 // request handling code goes here
33 Console.WriteLine("request handled in ConcreteHandler1");
34 }
35 else
36 {
37 // not handled here - pass on to next in the chain
38 if (successorHandler != null)
39 successorHandler.HandleRequest(request);
40 }
41 }
42 }
43
44 class ConcreteHandler2 : Handler
45 {
46 override public void HandleRequest(Request request)
47 {
48 // determine if we can handle the request
49 if (request.RequestType == 2) // some complex decision making!
50 {
51 // request handling code goes here
52 Console.WriteLine("request handled in ConcreteHandler2");
53 }
54 else
55 {
56 // not handled here - pass on to next in the chain
57 if (successorHandler != null)
58 successorHandler.HandleRequest(request);
59 }
60 }
61 }
62
63 class ConcreteHandler3 : Handler
64 {
65 override public void HandleRequest(Request request)
66 {
67 // determine if we can handle the request
68 if (request.RequestType == 3) // some complex decision making!
69 {
70 // request handling code goes here
71 Console.WriteLine("request handled in ConcreteHandler3");
72 }
73 else
74 {
75 // not handled here - pass on to next in the chain
76 if (successorHandler != null)
77 successorHandler.HandleRequest(request);
78 }
79 }
80 }
81
82 class Request
83 {
84 private int iRequestType;
85 private string strRequestParameters;
86
87 public Request(int requestType, string requestParameters)
88 {
89 iRequestType = requestType;
90 strRequestParameters = requestParameters;
91 }
92
93 public int RequestType
94 {
95 get
96 {
97 return iRequestType;
98 }
99 set
100 {
101 iRequestType = value;
102 }
103 }
104 }
105
106 /// <summary>
107 /// Summary description for Client.
108 /// </summary>
109 public class Client
110 {
111 public static int Main(string[] args)
112 {
113 // Set up chain (usually one need to be done once)
114 Handler firstHandler = new ConcreteHandler1();
115 Handler secondHandler = new ConcreteHandler2();
116 Handler thirdHandler = new ConcreteHandler3();
117 firstHandler.SetSuccessor(secondHandler);
118 secondHandler.SetSuccessor(thirdHandler);
119
120 // After setting up the chain of responsibility, we can
121 // now generate requests and pass then off to the
122 // chain to be handled
123
124 // generate and fire request
125 Request newRequest = new Request(2,"This are the request parameters");
126 firstHandler.HandleRequest(newRequest);
127
128 return 0;
129 }
130 }
131 }