File tree Expand file tree Collapse file tree 10 files changed +189
-0
lines changed
src/main/java/com/iluwatar Expand file tree Collapse file tree 10 files changed +189
-0
lines changed Original file line number Diff line number Diff line change 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 >
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 1+ package com .iluwatar ;
2+
3+ public enum RequestType {
4+
5+ DEFEND_CASTLE ,
6+ TORTURE_PRISONER ,
7+ COLLECT_TAX
8+
9+ }
Original file line number Diff line number Diff line change 3030 <module >facade</module >
3131 <module >flyweight</module >
3232 <module >proxy</module >
33+ <module >chain</module >
3334 </modules >
3435
3536<build >
You can’t perform that action at this time.
0 commit comments