Skip to content

Commit eb36033

Browse files
committed
iluwatar#590 Add explanation for Chain of Responsibility
1 parent 1b0f55c commit eb36033

File tree

9 files changed

+127
-275
lines changed

9 files changed

+127
-275
lines changed

chain/README.md

Lines changed: 125 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,131 @@ Avoid coupling the sender of a request to its receiver by giving
1616
more than one object a chance to handle the request. Chain the receiving
1717
objects and pass the request along the chain until an object handles it.
1818

19-
![alt text](./etc/chain_1.png "Chain of Responsibility")
19+
## Explanation
20+
21+
Real world example
22+
23+
> The Orc King gives loud orders to his army. The closest one to react is the commander, then officer and then soldier. The commander, officer and soldier here form a chain of responsibility.
24+
25+
In plain words
26+
27+
> It helps building a chain of objects. Request enters from one end and keeps going from object to object till it finds the suitable handler.
28+
29+
Wikipedia says
30+
31+
> In object-oriented design, the chain-of-responsibility pattern is a design pattern consisting of a source of command objects and a series of processing objects. Each processing object contains logic that defines the types of command objects that it can handle; the rest are passed to the next processing object in the chain.
32+
33+
**Programmatic Example**
34+
35+
Translating our example with orcs from above. First we have the request class
36+
37+
```
38+
public class Request {
39+
40+
private final RequestType requestType;
41+
private final String requestDescription;
42+
private boolean handled;
43+
44+
public Request(final RequestType requestType, final String requestDescription) {
45+
this.requestType = Objects.requireNonNull(requestType);
46+
this.requestDescription = Objects.requireNonNull(requestDescription);
47+
}
48+
49+
public String getRequestDescription() { return requestDescription; }
50+
51+
public RequestType getRequestType() { return requestType; }
52+
53+
public void markHandled() { this.handled = true; }
54+
55+
public boolean isHandled() { return this.handled; }
56+
57+
@Override
58+
public String toString() { return getRequestDescription(); }
59+
}
60+
61+
public enum RequestType {
62+
DEFEND_CASTLE, TORTURE_PRISONER, COLLECT_TAX
63+
}
64+
```
65+
66+
Then the request handler hierarchy
67+
68+
```
69+
public abstract class RequestHandler {
70+
private static final Logger LOGGER = LoggerFactory.getLogger(RequestHandler.class);
71+
private RequestHandler next;
72+
73+
public RequestHandler(RequestHandler next) {
74+
this.next = next;
75+
}
76+
77+
public void handleRequest(Request req) {
78+
if (next != null) {
79+
next.handleRequest(req);
80+
}
81+
}
82+
83+
protected void printHandling(Request req) {
84+
LOGGER.info("{} handling request \"{}\"", this, req);
85+
}
86+
87+
@Override
88+
public abstract String toString();
89+
}
90+
91+
public class OrcCommander extends RequestHandler {
92+
public OrcCommander(RequestHandler handler) {
93+
super(handler);
94+
}
95+
96+
@Override
97+
public void handleRequest(Request req) {
98+
if (req.getRequestType().equals(RequestType.DEFEND_CASTLE)) {
99+
printHandling(req);
100+
req.markHandled();
101+
} else {
102+
super.handleRequest(req);
103+
}
104+
}
105+
106+
@Override
107+
public String toString() {
108+
return "Orc commander";
109+
}
110+
}
111+
112+
// OrcOfficer and OrcSoldier are defined similarly as OrcCommander
113+
114+
```
115+
116+
Then we have the Orc King who gives the orders and forms the chain
117+
118+
```
119+
public class OrcKing {
120+
RequestHandler chain;
121+
122+
public OrcKing() {
123+
buildChain();
124+
}
125+
126+
private void buildChain() {
127+
chain = new OrcCommander(new OrcOfficer(new OrcSoldier(null)));
128+
}
129+
130+
public void makeRequest(Request req) {
131+
chain.handleRequest(req);
132+
}
133+
}
134+
```
135+
136+
Then it is used as follows
137+
138+
```
139+
OrcKing king = new OrcKing();
140+
king.makeRequest(new Request(RequestType.DEFEND_CASTLE, "defend castle")); // Orc commander handling request "defend castle"
141+
king.makeRequest(new Request(RequestType.TORTURE_PRISONER, "torture prisoner")); // Orc officer handling request "torture prisoner"
142+
king.makeRequest(new Request(RequestType.COLLECT_TAX, "collect tax")); // Orc soldier handling request "collect tax"
143+
```
20144

21145
## Applicability
22146
Use Chain of Responsibility when

chain/etc/chain.png

-30.3 KB
Binary file not shown.

chain/etc/chain.ucls

Lines changed: 0 additions & 109 deletions
This file was deleted.

chain/etc/chain.urm.puml

Lines changed: 0 additions & 61 deletions
This file was deleted.

chain/etc/chain_1.png

-60.2 KB
Binary file not shown.

pom.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,8 @@
473473
<param>decorator</param>
474474
<param>facade</param>
475475
<param>flyweight</param>
476+
<param>proxy</param>
477+
<param>chain</param>
476478
</skipForProjects>
477479
</configuration>
478480
</plugin>

proxy/etc/proxy.png

-22.4 KB
Binary file not shown.

proxy/etc/proxy.ucls

Lines changed: 0 additions & 72 deletions
This file was deleted.

0 commit comments

Comments
 (0)