X Tutup
Skip to content

Commit fa0acb4

Browse files
committed
iluwatar#107 Chain example JavaDoc improvements
1 parent b3b6479 commit fa0acb4

File tree

9 files changed

+229
-190
lines changed

9 files changed

+229
-190
lines changed
Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
1-
package com.iluwatar.chain;
2-
3-
/**
4-
*
5-
* Chain of Responsibility organizes request handlers (RequestHandler) into a
6-
* chain where each handler has a chance to act on the request on its turn. In
7-
* this example the king (OrcKing) makes requests and the military orcs
8-
* (OrcCommander, OrcOfficer, OrcSoldier) form the handler chain.
9-
*
10-
*/
11-
public class App {
12-
13-
public static void main(String[] args) {
14-
15-
OrcKing king = new OrcKing();
16-
king.makeRequest(new Request(RequestType.DEFEND_CASTLE, "defend castle"));
17-
king.makeRequest(new Request(RequestType.TORTURE_PRISONER,
18-
"torture prisoner"));
19-
king.makeRequest(new Request(RequestType.COLLECT_TAX, "collect tax"));
20-
21-
}
22-
}
1+
package com.iluwatar.chain;
2+
3+
/**
4+
*
5+
* Chain of Responsibility organizes request handlers ({@link RequestHandler}) into a
6+
* chain where each handler has a chance to act on the request on its turn. In
7+
* this example the king ({@link OrcKing}) makes requests and the military orcs
8+
* ({@link OrcCommander}, {@link OrcOfficer}, {@link OrcSoldier}) form the handler chain.
9+
*
10+
*/
11+
public class App {
12+
13+
/**
14+
* Program entry point
15+
* @param args command line args
16+
*/
17+
public static void main(String[] args) {
18+
19+
OrcKing king = new OrcKing();
20+
king.makeRequest(new Request(RequestType.DEFEND_CASTLE, "defend castle"));
21+
king.makeRequest(new Request(RequestType.TORTURE_PRISONER,
22+
"torture prisoner"));
23+
king.makeRequest(new Request(RequestType.COLLECT_TAX, "collect tax"));
24+
25+
}
26+
}
Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
1-
package com.iluwatar.chain;
2-
3-
public class OrcCommander extends RequestHandler {
4-
5-
public OrcCommander(RequestHandler handler) {
6-
super(handler);
7-
}
8-
9-
@Override
10-
public void handleRequest(Request req) {
11-
if (req.getRequestType().equals(RequestType.DEFEND_CASTLE)) {
12-
printHandling(req);
13-
} else {
14-
super.handleRequest(req);
15-
}
16-
}
17-
18-
@Override
19-
public String toString() {
20-
return "Orc commander";
21-
}
22-
}
1+
package com.iluwatar.chain;
2+
3+
/**
4+
*
5+
* OrcCommander
6+
*
7+
*/
8+
public class OrcCommander extends RequestHandler {
9+
10+
public OrcCommander(RequestHandler handler) {
11+
super(handler);
12+
}
13+
14+
@Override
15+
public void handleRequest(Request req) {
16+
if (req.getRequestType().equals(RequestType.DEFEND_CASTLE)) {
17+
printHandling(req);
18+
} else {
19+
super.handleRequest(req);
20+
}
21+
}
22+
23+
@Override
24+
public String toString() {
25+
return "Orc commander";
26+
}
27+
}
Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1-
package com.iluwatar.chain;
2-
3-
/**
4-
*
5-
* Makes requests that are handled by the chain.
6-
*
7-
*/
8-
public class OrcKing {
9-
10-
RequestHandler chain;
11-
12-
public OrcKing() {
13-
buildChain();
14-
}
15-
16-
private void buildChain() {
17-
chain = new OrcCommander(new OrcOfficer(new OrcSoldier(null)));
18-
}
19-
20-
public void makeRequest(Request req) {
21-
chain.handleRequest(req);
22-
}
23-
24-
}
1+
package com.iluwatar.chain;
2+
3+
/**
4+
*
5+
* OrcKing makes requests that are handled by the chain.
6+
*
7+
*/
8+
public class OrcKing {
9+
10+
RequestHandler chain;
11+
12+
public OrcKing() {
13+
buildChain();
14+
}
15+
16+
private void buildChain() {
17+
chain = new OrcCommander(new OrcOfficer(new OrcSoldier(null)));
18+
}
19+
20+
public void makeRequest(Request req) {
21+
chain.handleRequest(req);
22+
}
23+
24+
}
Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
1-
package com.iluwatar.chain;
2-
3-
public class OrcOfficer extends RequestHandler {
4-
5-
public OrcOfficer(RequestHandler handler) {
6-
super(handler);
7-
}
8-
9-
@Override
10-
public void handleRequest(Request req) {
11-
if (req.getRequestType().equals(RequestType.TORTURE_PRISONER)) {
12-
printHandling(req);
13-
} else {
14-
super.handleRequest(req);
15-
}
16-
}
17-
18-
@Override
19-
public String toString() {
20-
return "Orc officer";
21-
}
22-
23-
}
1+
package com.iluwatar.chain;
2+
3+
/**
4+
*
5+
* OrcOfficer
6+
*
7+
*/
8+
public class OrcOfficer extends RequestHandler {
9+
10+
public OrcOfficer(RequestHandler handler) {
11+
super(handler);
12+
}
13+
14+
@Override
15+
public void handleRequest(Request req) {
16+
if (req.getRequestType().equals(RequestType.TORTURE_PRISONER)) {
17+
printHandling(req);
18+
} else {
19+
super.handleRequest(req);
20+
}
21+
}
22+
23+
@Override
24+
public String toString() {
25+
return "Orc officer";
26+
}
27+
28+
}
Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
1-
package com.iluwatar.chain;
2-
3-
public class OrcSoldier extends RequestHandler {
4-
5-
public OrcSoldier(RequestHandler handler) {
6-
super(handler);
7-
}
8-
9-
@Override
10-
public void handleRequest(Request req) {
11-
if (req.getRequestType().equals(RequestType.COLLECT_TAX)) {
12-
printHandling(req);
13-
} else {
14-
super.handleRequest(req);
15-
}
16-
}
17-
18-
@Override
19-
public String toString() {
20-
return "Orc soldier";
21-
}
22-
}
1+
package com.iluwatar.chain;
2+
3+
/**
4+
*
5+
* OrcSoldier
6+
*
7+
*/
8+
public class OrcSoldier extends RequestHandler {
9+
10+
public OrcSoldier(RequestHandler handler) {
11+
super(handler);
12+
}
13+
14+
@Override
15+
public void handleRequest(Request req) {
16+
if (req.getRequestType().equals(RequestType.COLLECT_TAX)) {
17+
printHandling(req);
18+
} else {
19+
super.handleRequest(req);
20+
}
21+
}
22+
23+
@Override
24+
public String toString() {
25+
return "Orc soldier";
26+
}
27+
}
Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,38 @@
1-
package com.iluwatar.chain;
2-
3-
public class Request {
4-
5-
private String requestDescription;
6-
private RequestType requestType;
7-
8-
public Request(RequestType requestType, String requestDescription) {
9-
this.setRequestType(requestType);
10-
this.setRequestDescription(requestDescription);
11-
}
12-
13-
public String getRequestDescription() {
14-
return requestDescription;
15-
}
16-
17-
public void setRequestDescription(String requestDescription) {
18-
this.requestDescription = requestDescription;
19-
}
20-
21-
public RequestType getRequestType() {
22-
return requestType;
23-
}
24-
25-
public void setRequestType(RequestType requestType) {
26-
this.requestType = requestType;
27-
}
28-
29-
@Override
30-
public String toString() {
31-
return getRequestDescription();
32-
}
33-
}
1+
package com.iluwatar.chain;
2+
3+
/**
4+
*
5+
* Request
6+
*
7+
*/
8+
public class Request {
9+
10+
private String requestDescription;
11+
private RequestType requestType;
12+
13+
public Request(RequestType requestType, String requestDescription) {
14+
this.setRequestType(requestType);
15+
this.setRequestDescription(requestDescription);
16+
}
17+
18+
public String getRequestDescription() {
19+
return requestDescription;
20+
}
21+
22+
public void setRequestDescription(String requestDescription) {
23+
this.requestDescription = requestDescription;
24+
}
25+
26+
public RequestType getRequestType() {
27+
return requestType;
28+
}
29+
30+
public void setRequestType(RequestType requestType) {
31+
this.requestType = requestType;
32+
}
33+
34+
@Override
35+
public String toString() {
36+
return getRequestDescription();
37+
}
38+
}
Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
1-
package com.iluwatar.chain;
2-
3-
public abstract class RequestHandler {
4-
5-
private RequestHandler next;
6-
7-
public RequestHandler(RequestHandler next) {
8-
this.next = next;
9-
}
10-
11-
public void handleRequest(Request req) {
12-
if (next != null) {
13-
next.handleRequest(req);
14-
}
15-
}
16-
17-
protected void printHandling(Request req) {
18-
System.out.println(this + " handling request \"" + req + "\"");
19-
}
20-
21-
@Override
22-
public abstract String toString();
23-
}
1+
package com.iluwatar.chain;
2+
3+
/**
4+
*
5+
* RequestHandler
6+
*
7+
*/
8+
public abstract class RequestHandler {
9+
10+
private RequestHandler next;
11+
12+
public RequestHandler(RequestHandler next) {
13+
this.next = next;
14+
}
15+
16+
public void handleRequest(Request req) {
17+
if (next != null) {
18+
next.handleRequest(req);
19+
}
20+
}
21+
22+
protected void printHandling(Request req) {
23+
System.out.println(this + " handling request \"" + req + "\"");
24+
}
25+
26+
@Override
27+
public abstract String toString();
28+
}
Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1-
package com.iluwatar.chain;
2-
3-
public enum RequestType {
4-
5-
DEFEND_CASTLE, TORTURE_PRISONER, COLLECT_TAX
6-
7-
}
1+
package com.iluwatar.chain;
2+
3+
/**
4+
*
5+
* RequestType enumeration
6+
*
7+
*/
8+
public enum RequestType {
9+
10+
DEFEND_CASTLE, TORTURE_PRISONER, COLLECT_TAX
11+
12+
}

0 commit comments

Comments
 (0)
X Tutup