Skip to content

Commit e6342d5

Browse files
Merge pull request iluwatar#1 from iluwatar/master
upstream update
2 parents a745c5d + 6848dd0 commit e6342d5

58 files changed

Lines changed: 1804 additions & 681 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

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.

checkstyle.xml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,7 @@
8585
value="LITERAL_TRY, LITERAL_CATCH, LITERAL_FINALLY, LITERAL_IF, LITERAL_ELSE, LITERAL_SWITCH"/>
8686
</module>
8787
<module name="NeedBraces"/>
88-
<module name="LeftCurly">
89-
<property name="maxLineLength" value="120"/>
90-
</module>
88+
<module name="LeftCurly"/>
9189
<module name="RightCurly"/>
9290
<module name="RightCurly">
9391
<property name="option" value="alone"/>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
package com.iluwatar.dao;
24+
25+
/**
26+
*
27+
* Custom exception
28+
*
29+
*/
30+
public class CustomException extends Exception {
31+
32+
private static final long serialVersionUID = 1L;
33+
34+
public CustomException() {}
35+
36+
public CustomException(String message) {
37+
super(message);
38+
}
39+
40+
public CustomException(String message, Throwable cause) {
41+
super(message, cause);
42+
}
43+
}

0 commit comments

Comments
 (0)