1
1
---
2
2
layout : pattern
3
3
title : Chain of responsibility
4
- folder : chain
5
- permalink : /patterns/chain/
4
+ folder : chain-of-responsibility
5
+ permalink : /patterns/chain-of-responsibility /
6
6
categories : Behavioral
7
7
language : en
8
8
tags :
9
9
- Gang of Four
10
10
---
11
11
12
12
## Intent
13
+
13
14
Avoid coupling the sender of a request to its receiver by giving more than one object a chance to
14
15
handle the request. Chain the receiving objects and pass the request along the chain until an object
15
16
handles it.
16
17
17
18
## Explanation
18
19
19
- Real world example
20
+ Real- world example
20
21
21
22
> The Orc King gives loud orders to his army. The closest one to react is the commander, then
22
- > officer and then soldier. The commander, officer and soldier here form a chain of responsibility.
23
+ > an officer, and then a soldier. The commander, officer, and soldier form a chain of responsibility.
23
24
24
25
In plain words
25
26
@@ -35,7 +36,7 @@ Wikipedia says
35
36
36
37
** Programmatic Example**
37
38
38
- Translating our example with the orcs from above. First we have the ` Request ` class:
39
+ Translating our example with the orcs from above. First, we have the ` Request ` class:
39
40
40
41
``` java
41
42
public class Request {
@@ -66,7 +67,7 @@ public enum RequestType {
66
67
}
67
68
```
68
69
69
- Then the request handler hierarchy
70
+ Next, we show the request handler hierarchy.
70
71
71
72
``` java
72
73
@Slf4j
@@ -116,7 +117,7 @@ public class OrcCommander extends RequestHandler {
116
117
117
118
```
118
119
119
- Then we have the Orc King who gives the orders and forms the chain
120
+ Th Orc King gives the orders and forms the chain.
120
121
121
122
``` java
122
123
public class OrcKing {
@@ -136,18 +137,26 @@ public class OrcKing {
136
137
}
137
138
```
138
139
139
- Then it is used as follows
140
+ The chain of responsibility in action.
140
141
141
142
``` java
142
143
var king = new OrcKing ();
143
- king. makeRequest(new Request (RequestType . DEFEND_CASTLE , " defend castle" )); // Orc commander handling request "defend castle"
144
- king. makeRequest(new Request (RequestType . TORTURE_PRISONER , " torture prisoner" )); // Orc officer handling request "torture prisoner"
145
- king. makeRequest(new Request (RequestType . COLLECT_TAX , " collect tax" )); // Orc soldier handling request "collect tax"
144
+ king. makeRequest(new Request (RequestType . DEFEND_CASTLE , " defend castle" ));
145
+ king. makeRequest(new Request (RequestType . TORTURE_PRISONER , " torture prisoner" ));
146
+ king. makeRequest(new Request (RequestType . COLLECT_TAX , " collect tax" ));
147
+ ```
148
+
149
+ The console output.
150
+
151
+ ```
152
+ Orc commander handling request "defend castle"
153
+ Orc officer handling request "torture prisoner"
154
+ Orc soldier handling request "collect tax"
146
155
```
147
156
148
157
## Class diagram
149
158
150
- ![ alt text] ( ./etc/chain.urm.png " Chain of Responsibility class diagram ")
159
+ ![ alt text] ( ./etc/chain-of-responsibility .urm.png " Chain of Responsibility class diagram ")
151
160
152
161
## Applicability
153
162
@@ -157,7 +166,7 @@ Use Chain of Responsibility when
157
166
* You want to issue a request to one of several objects without specifying the receiver explicitly.
158
167
* The set of objects that can handle a request should be specified dynamically.
159
168
160
- ## Real world examples
169
+ ## Real- world examples
161
170
162
171
* [ java.util.logging.Logger#log()] ( http://docs.oracle.com/javase/8/docs/api/java/util/logging/Logger.html#log%28java.util.logging.Level,%20java.lang.String%29 )
163
172
* [ Apache Commons Chain] ( https://commons.apache.org/proper/commons-chain/index.html )
0 commit comments