Reformat business-delegate, callback, chain, command, composite, dao, decorator & dependency-injection patterns.

This commit is contained in:
Ankur Kaushal
2015-11-01 18:48:43 -05:00
parent 3af06a3a3a
commit 449340bd2b
54 changed files with 698 additions and 700 deletions

View File

@ -2,31 +2,30 @@ package com.iluwatar.chain;
/**
*
* The Chain of Responsibility pattern is a design pattern consisting 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. A mechanism also exists for
* adding new processing objects to the end of this chain.
* The Chain of Responsibility pattern is a design pattern consisting 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. A mechanism also exists for adding new processing objects to the end of this chain.
* <p>
* In this example we organize the request handlers ({@link RequestHandler}) into a
* chain where each handler has a chance to act on the request on its turn. Here
* the king ({@link OrcKing}) makes requests and the military orcs ({@link OrcCommander},
* {@link OrcOfficer}, {@link OrcSoldier}) form the handler chain.
* In this example we organize the request handlers ({@link RequestHandler}) into a chain where each
* handler has a chance to act on the request on its turn. Here the king ({@link OrcKing}) makes
* requests and the military orcs ({@link OrcCommander}, {@link OrcOfficer}, {@link OrcSoldier})
* form the handler chain.
*
*/
public class App {
/**
* Program entry point
* @param args command line args
*/
public static void main(String[] args) {
/**
* Program entry point
*
* @param args command line args
*/
public static void main(String[] args) {
OrcKing king = new OrcKing();
king.makeRequest(new Request(RequestType.DEFEND_CASTLE, "defend castle"));
king.makeRequest(new Request(RequestType.TORTURE_PRISONER,
"torture prisoner"));
king.makeRequest(new Request(RequestType.COLLECT_TAX, "collect tax"));
OrcKing king = new OrcKing();
king.makeRequest(new Request(RequestType.DEFEND_CASTLE, "defend castle"));
king.makeRequest(new Request(RequestType.TORTURE_PRISONER, "torture prisoner"));
king.makeRequest(new Request(RequestType.COLLECT_TAX, "collect tax"));
}
}
}

View File

@ -7,21 +7,21 @@ package com.iluwatar.chain;
*/
public class OrcCommander extends RequestHandler {
public OrcCommander(RequestHandler handler) {
super(handler);
}
public OrcCommander(RequestHandler handler) {
super(handler);
}
@Override
public void handleRequest(Request req) {
if (req.getRequestType().equals(RequestType.DEFEND_CASTLE)) {
printHandling(req);
} else {
super.handleRequest(req);
}
}
@Override
public void handleRequest(Request req) {
if (req.getRequestType().equals(RequestType.DEFEND_CASTLE)) {
printHandling(req);
} else {
super.handleRequest(req);
}
}
@Override
public String toString() {
return "Orc commander";
}
@Override
public String toString() {
return "Orc commander";
}
}

View File

@ -7,18 +7,18 @@ package com.iluwatar.chain;
*/
public class OrcKing {
RequestHandler chain;
RequestHandler chain;
public OrcKing() {
buildChain();
}
public OrcKing() {
buildChain();
}
private void buildChain() {
chain = new OrcCommander(new OrcOfficer(new OrcSoldier(null)));
}
private void buildChain() {
chain = new OrcCommander(new OrcOfficer(new OrcSoldier(null)));
}
public void makeRequest(Request req) {
chain.handleRequest(req);
}
public void makeRequest(Request req) {
chain.handleRequest(req);
}
}

View File

@ -7,22 +7,22 @@ package com.iluwatar.chain;
*/
public class OrcOfficer extends RequestHandler {
public OrcOfficer(RequestHandler handler) {
super(handler);
}
public OrcOfficer(RequestHandler handler) {
super(handler);
}
@Override
public void handleRequest(Request req) {
if (req.getRequestType().equals(RequestType.TORTURE_PRISONER)) {
printHandling(req);
} else {
super.handleRequest(req);
}
}
@Override
public void handleRequest(Request req) {
if (req.getRequestType().equals(RequestType.TORTURE_PRISONER)) {
printHandling(req);
} else {
super.handleRequest(req);
}
}
@Override
public String toString() {
return "Orc officer";
}
@Override
public String toString() {
return "Orc officer";
}
}

View File

@ -7,21 +7,21 @@ package com.iluwatar.chain;
*/
public class OrcSoldier extends RequestHandler {
public OrcSoldier(RequestHandler handler) {
super(handler);
}
public OrcSoldier(RequestHandler handler) {
super(handler);
}
@Override
public void handleRequest(Request req) {
if (req.getRequestType().equals(RequestType.COLLECT_TAX)) {
printHandling(req);
} else {
super.handleRequest(req);
}
}
@Override
public void handleRequest(Request req) {
if (req.getRequestType().equals(RequestType.COLLECT_TAX)) {
printHandling(req);
} else {
super.handleRequest(req);
}
}
@Override
public String toString() {
return "Orc soldier";
}
@Override
public String toString() {
return "Orc soldier";
}
}

View File

@ -7,32 +7,32 @@ package com.iluwatar.chain;
*/
public class Request {
private String requestDescription;
private RequestType requestType;
private String requestDescription;
private RequestType requestType;
public Request(RequestType requestType, String requestDescription) {
this.setRequestType(requestType);
this.setRequestDescription(requestDescription);
}
public Request(RequestType requestType, String requestDescription) {
this.setRequestType(requestType);
this.setRequestDescription(requestDescription);
}
public String getRequestDescription() {
return requestDescription;
}
public String getRequestDescription() {
return requestDescription;
}
public void setRequestDescription(String requestDescription) {
this.requestDescription = requestDescription;
}
public void setRequestDescription(String requestDescription) {
this.requestDescription = requestDescription;
}
public RequestType getRequestType() {
return requestType;
}
public RequestType getRequestType() {
return requestType;
}
public void setRequestType(RequestType requestType) {
this.requestType = requestType;
}
public void setRequestType(RequestType requestType) {
this.requestType = requestType;
}
@Override
public String toString() {
return getRequestDescription();
}
@Override
public String toString() {
return getRequestDescription();
}
}

View File

@ -7,22 +7,22 @@ package com.iluwatar.chain;
*/
public abstract class RequestHandler {
private RequestHandler next;
private RequestHandler next;
public RequestHandler(RequestHandler next) {
this.next = next;
}
public RequestHandler(RequestHandler next) {
this.next = next;
}
public void handleRequest(Request req) {
if (next != null) {
next.handleRequest(req);
}
}
public void handleRequest(Request req) {
if (next != null) {
next.handleRequest(req);
}
}
protected void printHandling(Request req) {
System.out.println(this + " handling request \"" + req + "\"");
}
protected void printHandling(Request req) {
System.out.println(this + " handling request \"" + req + "\"");
}
@Override
public abstract String toString();
@Override
public abstract String toString();
}

View File

@ -7,6 +7,6 @@ package com.iluwatar.chain;
*/
public enum RequestType {
DEFEND_CASTLE, TORTURE_PRISONER, COLLECT_TAX
DEFEND_CASTLE, TORTURE_PRISONER, COLLECT_TAX
}

View File

@ -11,9 +11,9 @@ import com.iluwatar.chain.App;
*/
public class AppTest {
@Test
public void test() {
String[] args = {};
App.main(args);
}
@Test
public void test() {
String[] args = {};
App.main(args);
}
}