X Tutup
Skip to content

Commit 9e7db12

Browse files
committed
added chain of responsibility sample
1 parent a53c217 commit 9e7db12

File tree

10 files changed

+189
-0
lines changed

10 files changed

+189
-0
lines changed

chain/pom.xml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0"?>
2+
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>com.iluwatar</groupId>
7+
<artifactId>java-design-patterns</artifactId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<groupId>com.iluwatar</groupId>
11+
<artifactId>chain</artifactId>
12+
<version>1.0-SNAPSHOT</version>
13+
<name>chain</name>
14+
<url>http://maven.apache.org</url>
15+
<dependencies>
16+
<dependency>
17+
<groupId>junit</groupId>
18+
<artifactId>junit</artifactId>
19+
<version>3.8.1</version>
20+
<scope>test</scope>
21+
</dependency>
22+
</dependencies>
23+
</project>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.iluwatar;
2+
3+
public class App
4+
{
5+
public static void main( String[] args )
6+
{
7+
8+
OrcKing king = new OrcKing();
9+
king.makeRequest(new Request(RequestType.DEFEND_CASTLE, "defend castle"));
10+
king.makeRequest(new Request(RequestType.TORTURE_PRISONER, "torture prisoner"));
11+
king.makeRequest(new Request(RequestType.COLLECT_TAX, "collect tax"));
12+
13+
}
14+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.iluwatar;
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+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.iluwatar;
2+
3+
public class OrcKing {
4+
5+
RequestHandler chain;
6+
7+
public OrcKing() {
8+
buildChain();
9+
}
10+
11+
private void buildChain() {
12+
chain = new OrcCommander(new OrcOfficer(new OrcSoldier(null)));
13+
}
14+
15+
public void makeRequest(Request req) {
16+
chain.handleRequest(req);
17+
}
18+
19+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.iluwatar;
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+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.iluwatar;
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+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.iluwatar;
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+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.iluwatar;
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+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.iluwatar;
2+
3+
public enum RequestType {
4+
5+
DEFEND_CASTLE,
6+
TORTURE_PRISONER,
7+
COLLECT_TAX
8+
9+
}

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
<module>facade</module>
3131
<module>flyweight</module>
3232
<module>proxy</module>
33+
<module>chain</module>
3334
</modules>
3435

3536
<build>

0 commit comments

Comments
 (0)
X Tutup