Reformat business-delegate, callback, chain, command, composite, dao, decorator & dependency-injection patterns.
This commit is contained in:
parent
3af06a3a3a
commit
449340bd2b
@ -2,34 +2,36 @@ package com.iluwatar.business.delegate;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* The Business Delegate pattern adds an abstraction layer between the presentation and business tiers.
|
* The Business Delegate pattern adds an abstraction layer between the presentation and business
|
||||||
* By using the pattern we gain loose coupling between the tiers. The Business Delegate encapsulates
|
* tiers. By using the pattern we gain loose coupling between the tiers. The Business Delegate
|
||||||
* knowledge about how to locate, connect to, and interact with the business objects that make up
|
* encapsulates knowledge about how to locate, connect to, and interact with the business objects
|
||||||
* the application.
|
* that make up the application.
|
||||||
* <p>
|
* <p>
|
||||||
* Some of the services the Business Delegate uses are instantiated directly, and some can be retrieved
|
* Some of the services the Business Delegate uses are instantiated directly, and some can be
|
||||||
* through service lookups. The Business Delegate itself may contain business logic too potentially tying
|
* retrieved through service lookups. The Business Delegate itself may contain business logic too
|
||||||
* together multiple service calls, exception handling, retrying etc.
|
* potentially tying together multiple service calls, exception handling, retrying etc.
|
||||||
* <p>
|
* <p>
|
||||||
* In this example the client ({@link Client}) utilizes a business delegate ({@link BusinessDelegate}) to execute a task.
|
* In this example the client ({@link Client}) utilizes a business delegate (
|
||||||
* The Business Delegate then selects the appropriate service and makes the service call.
|
* {@link BusinessDelegate}) to execute a task. The Business Delegate then selects the appropriate
|
||||||
|
* service and makes the service call.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class App {
|
public class App {
|
||||||
|
|
||||||
/**
|
|
||||||
* Program entry point
|
|
||||||
* @param args command line args
|
|
||||||
*/
|
|
||||||
public static void main(String[] args) {
|
|
||||||
|
|
||||||
BusinessDelegate businessDelegate = new BusinessDelegate();
|
|
||||||
businessDelegate.setServiceType(ServiceType.EJB);
|
|
||||||
|
|
||||||
Client client = new Client(businessDelegate);
|
/**
|
||||||
client.doTask();
|
* Program entry point
|
||||||
|
*
|
||||||
|
* @param args command line args
|
||||||
|
*/
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
businessDelegate.setServiceType(ServiceType.JMS);
|
BusinessDelegate businessDelegate = new BusinessDelegate();
|
||||||
client.doTask();
|
businessDelegate.setServiceType(ServiceType.EJB);
|
||||||
}
|
|
||||||
|
Client client = new Client(businessDelegate);
|
||||||
|
client.doTask();
|
||||||
|
|
||||||
|
businessDelegate.setServiceType(ServiceType.JMS);
|
||||||
|
client.doTask();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,17 +6,17 @@ package com.iluwatar.business.delegate;
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class BusinessDelegate {
|
public class BusinessDelegate {
|
||||||
|
|
||||||
private BusinessLookup lookupService = new BusinessLookup();
|
|
||||||
private BusinessService businessService;
|
|
||||||
private ServiceType serviceType;
|
|
||||||
|
|
||||||
public void setServiceType(ServiceType serviceType) {
|
private BusinessLookup lookupService = new BusinessLookup();
|
||||||
this.serviceType = serviceType;
|
private BusinessService businessService;
|
||||||
}
|
private ServiceType serviceType;
|
||||||
|
|
||||||
public void doTask() {
|
public void setServiceType(ServiceType serviceType) {
|
||||||
businessService = lookupService.getBusinessService(serviceType);
|
this.serviceType = serviceType;
|
||||||
businessService.doProcessing();
|
}
|
||||||
}
|
|
||||||
|
public void doTask() {
|
||||||
|
businessService = lookupService.getBusinessService(serviceType);
|
||||||
|
businessService.doProcessing();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,11 +7,11 @@ package com.iluwatar.business.delegate;
|
|||||||
*/
|
*/
|
||||||
public class BusinessLookup {
|
public class BusinessLookup {
|
||||||
|
|
||||||
public BusinessService getBusinessService(ServiceType serviceType) {
|
public BusinessService getBusinessService(ServiceType serviceType) {
|
||||||
if (serviceType.equals(ServiceType.EJB)) {
|
if (serviceType.equals(ServiceType.EJB)) {
|
||||||
return new EjbService();
|
return new EjbService();
|
||||||
} else {
|
} else {
|
||||||
return new JmsService();
|
return new JmsService();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,5 +7,5 @@ package com.iluwatar.business.delegate;
|
|||||||
*/
|
*/
|
||||||
public interface BusinessService {
|
public interface BusinessService {
|
||||||
|
|
||||||
void doProcessing();
|
void doProcessing();
|
||||||
}
|
}
|
||||||
|
@ -7,13 +7,13 @@ package com.iluwatar.business.delegate;
|
|||||||
*/
|
*/
|
||||||
public class Client {
|
public class Client {
|
||||||
|
|
||||||
private BusinessDelegate businessDelegate;
|
private BusinessDelegate businessDelegate;
|
||||||
|
|
||||||
public Client(BusinessDelegate businessDelegate) {
|
public Client(BusinessDelegate businessDelegate) {
|
||||||
this.businessDelegate = businessDelegate;
|
this.businessDelegate = businessDelegate;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void doTask() {
|
public void doTask() {
|
||||||
businessDelegate.doTask();
|
businessDelegate.doTask();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,8 +7,8 @@ package com.iluwatar.business.delegate;
|
|||||||
*/
|
*/
|
||||||
public class EjbService implements BusinessService {
|
public class EjbService implements BusinessService {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void doProcessing() {
|
public void doProcessing() {
|
||||||
System.out.println("EjbService is now processing");
|
System.out.println("EjbService is now processing");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,8 +7,8 @@ package com.iluwatar.business.delegate;
|
|||||||
*/
|
*/
|
||||||
public class JmsService implements BusinessService {
|
public class JmsService implements BusinessService {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void doProcessing() {
|
public void doProcessing() {
|
||||||
System.out.println("JmsService is now processing");
|
System.out.println("JmsService is now processing");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,6 @@ package com.iluwatar.business.delegate;
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public enum ServiceType {
|
public enum ServiceType {
|
||||||
|
|
||||||
EJB, JMS;
|
EJB, JMS;
|
||||||
}
|
}
|
||||||
|
@ -10,10 +10,10 @@ import com.iluwatar.business.delegate.App;
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class AppTest {
|
public class AppTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void test() {
|
public void test() {
|
||||||
String[] args = {};
|
String[] args = {};
|
||||||
App.main(args);
|
App.main(args);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,20 +2,21 @@ package com.iluwatar.callback;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Callback pattern is more native for functional languages where functions are treated as first-class citizens.
|
* Callback pattern is more native for functional languages where functions are treated as
|
||||||
* Prior to Java 8 callbacks can be simulated using simple (alike command) interfaces.
|
* first-class citizens. Prior to Java 8 callbacks can be simulated using simple (alike command)
|
||||||
|
* interfaces.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class App {
|
public class App {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
Task task = new SimpleTask();
|
Task task = new SimpleTask();
|
||||||
Callback callback = new Callback() {
|
Callback callback = new Callback() {
|
||||||
@Override
|
@Override
|
||||||
public void call() {
|
public void call() {
|
||||||
System.out.println("I'm done now.");
|
System.out.println("I'm done now.");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
task.executeWith(callback);
|
task.executeWith(callback);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,5 +7,5 @@ package com.iluwatar.callback;
|
|||||||
*/
|
*/
|
||||||
public interface Callback {
|
public interface Callback {
|
||||||
|
|
||||||
public void call();
|
public void call();
|
||||||
}
|
}
|
||||||
|
@ -7,9 +7,8 @@ package com.iluwatar.callback;
|
|||||||
*/
|
*/
|
||||||
public class SimpleTask extends Task {
|
public class SimpleTask extends Task {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void execute() {
|
public void execute() {
|
||||||
System.out.println("Perform some important activity and after call the callback method.");
|
System.out.println("Perform some important activity and after call the callback method.");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -7,12 +7,12 @@ package com.iluwatar.callback;
|
|||||||
*/
|
*/
|
||||||
public abstract class Task {
|
public abstract class Task {
|
||||||
|
|
||||||
public final void executeWith(Callback callback) {
|
public final void executeWith(Callback callback) {
|
||||||
execute();
|
execute();
|
||||||
if (callback != null) {
|
if (callback != null) {
|
||||||
callback.call();
|
callback.call();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract void execute();
|
public abstract void execute();
|
||||||
}
|
}
|
||||||
|
@ -5,35 +5,35 @@ import org.junit.Test;
|
|||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add a field as a counter. Every time the callback method is called increment this
|
* Add a field as a counter. Every time the callback method is called increment this field. Unit
|
||||||
* field. Unit test checks that the field is being incremented.
|
* test checks that the field is being incremented.
|
||||||
*
|
*
|
||||||
* Could be done with mock objects as well where the call method call is verified.
|
* Could be done with mock objects as well where the call method call is verified.
|
||||||
*/
|
*/
|
||||||
public class AppTest {
|
public class AppTest {
|
||||||
|
|
||||||
private Integer callingCount = 0;
|
private Integer callingCount = 0;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void test() {
|
public void test() {
|
||||||
Callback callback = new Callback() {
|
Callback callback = new Callback() {
|
||||||
@Override
|
@Override
|
||||||
public void call() {
|
public void call() {
|
||||||
callingCount++;
|
callingCount++;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
Task task = new SimpleTask();
|
Task task = new SimpleTask();
|
||||||
|
|
||||||
assertEquals("Initial calling count of 0", new Integer(0), callingCount);
|
assertEquals("Initial calling count of 0", new Integer(0), callingCount);
|
||||||
|
|
||||||
task.executeWith(callback);
|
task.executeWith(callback);
|
||||||
|
|
||||||
assertEquals("Callback called once", new Integer(1), callingCount);
|
assertEquals("Callback called once", new Integer(1), callingCount);
|
||||||
|
|
||||||
task.executeWith(callback);
|
task.executeWith(callback);
|
||||||
|
|
||||||
assertEquals("Callback called twice", new Integer(2), callingCount);
|
assertEquals("Callback called twice", new Integer(2), callingCount);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,31 +2,30 @@ package com.iluwatar.chain;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* The Chain of Responsibility pattern is a design pattern consisting of command
|
* The Chain of Responsibility pattern is a design pattern consisting of command objects and a
|
||||||
* objects and a series of processing objects. Each processing object contains
|
* series of processing objects. Each processing object contains logic that defines the types of
|
||||||
* logic that defines the types of command objects that it can handle; the rest are
|
* command objects that it can handle; the rest are passed to the next processing object in the
|
||||||
* passed to the next processing object in the chain. A mechanism also exists for
|
* chain. A mechanism also exists for adding new processing objects to the end of this chain.
|
||||||
* adding new processing objects to the end of this chain.
|
|
||||||
* <p>
|
* <p>
|
||||||
* In this example we organize the request handlers ({@link RequestHandler}) into a
|
* In this example we organize the request handlers ({@link RequestHandler}) into a chain where each
|
||||||
* chain where each handler has a chance to act on the request on its turn. Here
|
* handler has a chance to act on the request on its turn. Here the king ({@link OrcKing}) makes
|
||||||
* the king ({@link OrcKing}) makes requests and the military orcs ({@link OrcCommander},
|
* requests and the military orcs ({@link OrcCommander}, {@link OrcOfficer}, {@link OrcSoldier})
|
||||||
* {@link OrcOfficer}, {@link OrcSoldier}) form the handler chain.
|
* form the handler chain.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class App {
|
public class App {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Program entry point
|
* Program entry point
|
||||||
* @param args command line args
|
*
|
||||||
*/
|
* @param args command line args
|
||||||
public static void main(String[] args) {
|
*/
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
OrcKing king = new OrcKing();
|
OrcKing king = new OrcKing();
|
||||||
king.makeRequest(new Request(RequestType.DEFEND_CASTLE, "defend castle"));
|
king.makeRequest(new Request(RequestType.DEFEND_CASTLE, "defend castle"));
|
||||||
king.makeRequest(new Request(RequestType.TORTURE_PRISONER,
|
king.makeRequest(new Request(RequestType.TORTURE_PRISONER, "torture prisoner"));
|
||||||
"torture prisoner"));
|
king.makeRequest(new Request(RequestType.COLLECT_TAX, "collect tax"));
|
||||||
king.makeRequest(new Request(RequestType.COLLECT_TAX, "collect tax"));
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,21 +7,21 @@ package com.iluwatar.chain;
|
|||||||
*/
|
*/
|
||||||
public class OrcCommander extends RequestHandler {
|
public class OrcCommander extends RequestHandler {
|
||||||
|
|
||||||
public OrcCommander(RequestHandler handler) {
|
public OrcCommander(RequestHandler handler) {
|
||||||
super(handler);
|
super(handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handleRequest(Request req) {
|
public void handleRequest(Request req) {
|
||||||
if (req.getRequestType().equals(RequestType.DEFEND_CASTLE)) {
|
if (req.getRequestType().equals(RequestType.DEFEND_CASTLE)) {
|
||||||
printHandling(req);
|
printHandling(req);
|
||||||
} else {
|
} else {
|
||||||
super.handleRequest(req);
|
super.handleRequest(req);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "Orc commander";
|
return "Orc commander";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,18 +7,18 @@ package com.iluwatar.chain;
|
|||||||
*/
|
*/
|
||||||
public class OrcKing {
|
public class OrcKing {
|
||||||
|
|
||||||
RequestHandler chain;
|
RequestHandler chain;
|
||||||
|
|
||||||
public OrcKing() {
|
public OrcKing() {
|
||||||
buildChain();
|
buildChain();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void buildChain() {
|
private void buildChain() {
|
||||||
chain = new OrcCommander(new OrcOfficer(new OrcSoldier(null)));
|
chain = new OrcCommander(new OrcOfficer(new OrcSoldier(null)));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void makeRequest(Request req) {
|
public void makeRequest(Request req) {
|
||||||
chain.handleRequest(req);
|
chain.handleRequest(req);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -7,22 +7,22 @@ package com.iluwatar.chain;
|
|||||||
*/
|
*/
|
||||||
public class OrcOfficer extends RequestHandler {
|
public class OrcOfficer extends RequestHandler {
|
||||||
|
|
||||||
public OrcOfficer(RequestHandler handler) {
|
public OrcOfficer(RequestHandler handler) {
|
||||||
super(handler);
|
super(handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handleRequest(Request req) {
|
public void handleRequest(Request req) {
|
||||||
if (req.getRequestType().equals(RequestType.TORTURE_PRISONER)) {
|
if (req.getRequestType().equals(RequestType.TORTURE_PRISONER)) {
|
||||||
printHandling(req);
|
printHandling(req);
|
||||||
} else {
|
} else {
|
||||||
super.handleRequest(req);
|
super.handleRequest(req);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "Orc officer";
|
return "Orc officer";
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -7,21 +7,21 @@ package com.iluwatar.chain;
|
|||||||
*/
|
*/
|
||||||
public class OrcSoldier extends RequestHandler {
|
public class OrcSoldier extends RequestHandler {
|
||||||
|
|
||||||
public OrcSoldier(RequestHandler handler) {
|
public OrcSoldier(RequestHandler handler) {
|
||||||
super(handler);
|
super(handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handleRequest(Request req) {
|
public void handleRequest(Request req) {
|
||||||
if (req.getRequestType().equals(RequestType.COLLECT_TAX)) {
|
if (req.getRequestType().equals(RequestType.COLLECT_TAX)) {
|
||||||
printHandling(req);
|
printHandling(req);
|
||||||
} else {
|
} else {
|
||||||
super.handleRequest(req);
|
super.handleRequest(req);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "Orc soldier";
|
return "Orc soldier";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,32 +7,32 @@ package com.iluwatar.chain;
|
|||||||
*/
|
*/
|
||||||
public class Request {
|
public class Request {
|
||||||
|
|
||||||
private String requestDescription;
|
private String requestDescription;
|
||||||
private RequestType requestType;
|
private RequestType requestType;
|
||||||
|
|
||||||
public Request(RequestType requestType, String requestDescription) {
|
public Request(RequestType requestType, String requestDescription) {
|
||||||
this.setRequestType(requestType);
|
this.setRequestType(requestType);
|
||||||
this.setRequestDescription(requestDescription);
|
this.setRequestDescription(requestDescription);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getRequestDescription() {
|
public String getRequestDescription() {
|
||||||
return requestDescription;
|
return requestDescription;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setRequestDescription(String requestDescription) {
|
public void setRequestDescription(String requestDescription) {
|
||||||
this.requestDescription = requestDescription;
|
this.requestDescription = requestDescription;
|
||||||
}
|
}
|
||||||
|
|
||||||
public RequestType getRequestType() {
|
public RequestType getRequestType() {
|
||||||
return requestType;
|
return requestType;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setRequestType(RequestType requestType) {
|
public void setRequestType(RequestType requestType) {
|
||||||
this.requestType = requestType;
|
this.requestType = requestType;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return getRequestDescription();
|
return getRequestDescription();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,22 +7,22 @@ package com.iluwatar.chain;
|
|||||||
*/
|
*/
|
||||||
public abstract class RequestHandler {
|
public abstract class RequestHandler {
|
||||||
|
|
||||||
private RequestHandler next;
|
private RequestHandler next;
|
||||||
|
|
||||||
public RequestHandler(RequestHandler next) {
|
public RequestHandler(RequestHandler next) {
|
||||||
this.next = next;
|
this.next = next;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void handleRequest(Request req) {
|
public void handleRequest(Request req) {
|
||||||
if (next != null) {
|
if (next != null) {
|
||||||
next.handleRequest(req);
|
next.handleRequest(req);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void printHandling(Request req) {
|
protected void printHandling(Request req) {
|
||||||
System.out.println(this + " handling request \"" + req + "\"");
|
System.out.println(this + " handling request \"" + req + "\"");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public abstract String toString();
|
public abstract String toString();
|
||||||
}
|
}
|
||||||
|
@ -7,6 +7,6 @@ package com.iluwatar.chain;
|
|||||||
*/
|
*/
|
||||||
public enum RequestType {
|
public enum RequestType {
|
||||||
|
|
||||||
DEFEND_CASTLE, TORTURE_PRISONER, COLLECT_TAX
|
DEFEND_CASTLE, TORTURE_PRISONER, COLLECT_TAX
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -11,9 +11,9 @@ import com.iluwatar.chain.App;
|
|||||||
*/
|
*/
|
||||||
public class AppTest {
|
public class AppTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void test() {
|
public void test() {
|
||||||
String[] args = {};
|
String[] args = {};
|
||||||
App.main(args);
|
App.main(args);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,52 +2,54 @@ package com.iluwatar.command;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* The Command pattern is a behavioral design pattern in which an object is used to encapsulate all information
|
* The Command pattern is a behavioral design pattern in which an object is used to encapsulate all
|
||||||
* needed to perform an action or trigger an event at a later time. This information includes the method name,
|
* information needed to perform an action or trigger an event at a later time. This information
|
||||||
* the object that owns the method and values for the method parameters.
|
* includes the method name, the object that owns the method and values for the method parameters.
|
||||||
* <p>
|
* <p>
|
||||||
* Four terms always associated with the command pattern are command, receiver, invoker and client. A command
|
* Four terms always associated with the command pattern are command, receiver, invoker and client.
|
||||||
* object (spell) knows about the receiver (target) and invokes a method of the receiver. Values for parameters of
|
* A command object (spell) knows about the receiver (target) and invokes a method of the receiver.
|
||||||
* the receiver method are stored in the command. The receiver then does the work. An invoker object (wizard)
|
* Values for parameters of the receiver method are stored in the command. The receiver then does
|
||||||
* knows how to execute a command, and optionally does bookkeeping about the command execution. The invoker
|
* the work. An invoker object (wizard) knows how to execute a command, and optionally does
|
||||||
* does not know anything about a concrete command, it knows only about command interface. Both an invoker object
|
* bookkeeping about the command execution. The invoker does not know anything about a concrete
|
||||||
* and several command objects are held by a client object (app). The client decides which commands to execute at
|
* command, it knows only about command interface. Both an invoker object and several command
|
||||||
* which points. To execute a command, it passes the command object to the invoker object.
|
* objects are held by a client object (app). The client decides which commands to execute at which
|
||||||
|
* points. To execute a command, it passes the command object to the invoker object.
|
||||||
* <p>
|
* <p>
|
||||||
* In other words, in this example the wizard casts spells on the goblin. The wizard keeps track of the previous
|
* In other words, in this example the wizard casts spells on the goblin. The wizard keeps track of
|
||||||
* spells cast, so it is easy to undo them. In addition, the wizard keeps track of the spells undone, so they
|
* the previous spells cast, so it is easy to undo them. In addition, the wizard keeps track of the
|
||||||
* can be redone.
|
* spells undone, so they can be redone.
|
||||||
*
|
*
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class App {
|
public class App {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Program entry point
|
* Program entry point
|
||||||
* @param args command line args
|
*
|
||||||
*/
|
* @param args command line args
|
||||||
public static void main(String[] args) {
|
*/
|
||||||
Wizard wizard = new Wizard();
|
public static void main(String[] args) {
|
||||||
Goblin goblin = new Goblin();
|
Wizard wizard = new Wizard();
|
||||||
|
Goblin goblin = new Goblin();
|
||||||
|
|
||||||
goblin.printStatus();
|
goblin.printStatus();
|
||||||
|
|
||||||
wizard.castSpell(new ShrinkSpell(), goblin);
|
wizard.castSpell(new ShrinkSpell(), goblin);
|
||||||
goblin.printStatus();
|
goblin.printStatus();
|
||||||
|
|
||||||
wizard.castSpell(new InvisibilitySpell(), goblin);
|
wizard.castSpell(new InvisibilitySpell(), goblin);
|
||||||
goblin.printStatus();
|
goblin.printStatus();
|
||||||
|
|
||||||
wizard.undoLastSpell();
|
wizard.undoLastSpell();
|
||||||
goblin.printStatus();
|
goblin.printStatus();
|
||||||
|
|
||||||
wizard.undoLastSpell();
|
wizard.undoLastSpell();
|
||||||
goblin.printStatus();
|
goblin.printStatus();
|
||||||
|
|
||||||
wizard.redoLastSpell();
|
wizard.redoLastSpell();
|
||||||
goblin.printStatus();
|
goblin.printStatus();
|
||||||
|
|
||||||
wizard.redoLastSpell();
|
wizard.redoLastSpell();
|
||||||
goblin.printStatus();
|
goblin.printStatus();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,13 +7,13 @@ package com.iluwatar.command;
|
|||||||
*/
|
*/
|
||||||
public abstract class Command {
|
public abstract class Command {
|
||||||
|
|
||||||
public abstract void execute(Target target);
|
public abstract void execute(Target target);
|
||||||
|
|
||||||
public abstract void undo();
|
public abstract void undo();
|
||||||
|
|
||||||
public abstract void redo();
|
public abstract void redo();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public abstract String toString();
|
public abstract String toString();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -7,14 +7,14 @@ package com.iluwatar.command;
|
|||||||
*/
|
*/
|
||||||
public class Goblin extends Target {
|
public class Goblin extends Target {
|
||||||
|
|
||||||
public Goblin() {
|
public Goblin() {
|
||||||
setSize(Size.NORMAL);
|
setSize(Size.NORMAL);
|
||||||
setVisibility(Visibility.VISIBLE);
|
setVisibility(Visibility.VISIBLE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "Goblin";
|
return "Goblin";
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -7,30 +7,30 @@ package com.iluwatar.command;
|
|||||||
*/
|
*/
|
||||||
public class InvisibilitySpell extends Command {
|
public class InvisibilitySpell extends Command {
|
||||||
|
|
||||||
private Target target;
|
private Target target;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void execute(Target target) {
|
public void execute(Target target) {
|
||||||
target.setVisibility(Visibility.INVISIBLE);
|
target.setVisibility(Visibility.INVISIBLE);
|
||||||
this.target = target;
|
this.target = target;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void undo() {
|
public void undo() {
|
||||||
if (target != null) {
|
if (target != null) {
|
||||||
target.setVisibility(Visibility.VISIBLE);
|
target.setVisibility(Visibility.VISIBLE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void redo() {
|
public void redo() {
|
||||||
if (target != null) {
|
if (target != null) {
|
||||||
target.setVisibility(Visibility.INVISIBLE);
|
target.setVisibility(Visibility.INVISIBLE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "Invisibility spell";
|
return "Invisibility spell";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,32 +7,32 @@ package com.iluwatar.command;
|
|||||||
*/
|
*/
|
||||||
public class ShrinkSpell extends Command {
|
public class ShrinkSpell extends Command {
|
||||||
|
|
||||||
private Size oldSize;
|
private Size oldSize;
|
||||||
private Target target;
|
private Target target;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void execute(Target target) {
|
public void execute(Target target) {
|
||||||
oldSize = target.getSize();
|
oldSize = target.getSize();
|
||||||
target.setSize(Size.SMALL);
|
target.setSize(Size.SMALL);
|
||||||
this.target = target;
|
this.target = target;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void undo() {
|
public void undo() {
|
||||||
if (oldSize != null && target != null) {
|
if (oldSize != null && target != null) {
|
||||||
Size temp = target.getSize();
|
Size temp = target.getSize();
|
||||||
target.setSize(oldSize);
|
target.setSize(oldSize);
|
||||||
oldSize = temp;
|
oldSize = temp;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void redo() {
|
public void redo() {
|
||||||
undo();
|
undo();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "Shrink spell";
|
return "Shrink spell";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,16 +7,16 @@ package com.iluwatar.command;
|
|||||||
*/
|
*/
|
||||||
public enum Size {
|
public enum Size {
|
||||||
|
|
||||||
SMALL("small"), NORMAL("normal"), LARGE("large"), UNDEFINED("");
|
SMALL("small"), NORMAL("normal"), LARGE("large"), UNDEFINED("");
|
||||||
|
|
||||||
private String title;
|
|
||||||
|
|
||||||
Size(String title) {
|
private String title;
|
||||||
this.title = title;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
Size(String title) {
|
||||||
public String toString() {
|
this.title = title;
|
||||||
return title;
|
}
|
||||||
}
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return title;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,32 +7,32 @@ package com.iluwatar.command;
|
|||||||
*/
|
*/
|
||||||
public abstract class Target {
|
public abstract class Target {
|
||||||
|
|
||||||
private Size size;
|
private Size size;
|
||||||
|
|
||||||
private Visibility visibility;
|
private Visibility visibility;
|
||||||
|
|
||||||
public Size getSize() {
|
public Size getSize() {
|
||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setSize(Size size) {
|
public void setSize(Size size) {
|
||||||
this.size = size;
|
this.size = size;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Visibility getVisibility() {
|
public Visibility getVisibility() {
|
||||||
return visibility;
|
return visibility;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setVisibility(Visibility visibility) {
|
public void setVisibility(Visibility visibility) {
|
||||||
this.visibility = visibility;
|
this.visibility = visibility;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public abstract String toString();
|
public abstract String toString();
|
||||||
|
|
||||||
public void printStatus() {
|
public void printStatus() {
|
||||||
System.out.println(String.format("%s, [size=%s] [visibility=%s]", this,
|
System.out.println(String.format("%s, [size=%s] [visibility=%s]", this, getSize(),
|
||||||
getSize(), getVisibility()));
|
getVisibility()));
|
||||||
System.out.println();
|
System.out.println();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,16 +7,16 @@ package com.iluwatar.command;
|
|||||||
*/
|
*/
|
||||||
public enum Visibility {
|
public enum Visibility {
|
||||||
|
|
||||||
VISIBLE("visible"), INVISIBLE("invisible"), UNDEFINED("");
|
VISIBLE("visible"), INVISIBLE("invisible"), UNDEFINED("");
|
||||||
|
|
||||||
private String title;
|
private String title;
|
||||||
|
|
||||||
Visibility(String title) {
|
Visibility(String title) {
|
||||||
this.title = title;
|
this.title = title;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return title;
|
return title;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,38 +10,37 @@ import java.util.LinkedList;
|
|||||||
*/
|
*/
|
||||||
public class Wizard {
|
public class Wizard {
|
||||||
|
|
||||||
private Deque<Command> undoStack = new LinkedList<>();
|
private Deque<Command> undoStack = new LinkedList<>();
|
||||||
private Deque<Command> redoStack = new LinkedList<>();
|
private Deque<Command> redoStack = new LinkedList<>();
|
||||||
|
|
||||||
public Wizard() {
|
public Wizard() {}
|
||||||
}
|
|
||||||
|
|
||||||
public void castSpell(Command command, Target target) {
|
public void castSpell(Command command, Target target) {
|
||||||
System.out.println(this + " casts " + command + " at " + target);
|
System.out.println(this + " casts " + command + " at " + target);
|
||||||
command.execute(target);
|
command.execute(target);
|
||||||
undoStack.offerLast(command);
|
undoStack.offerLast(command);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void undoLastSpell() {
|
public void undoLastSpell() {
|
||||||
if (!undoStack.isEmpty()) {
|
if (!undoStack.isEmpty()) {
|
||||||
Command previousSpell = undoStack.pollLast();
|
Command previousSpell = undoStack.pollLast();
|
||||||
redoStack.offerLast(previousSpell);
|
redoStack.offerLast(previousSpell);
|
||||||
System.out.println(this + " undoes " + previousSpell);
|
System.out.println(this + " undoes " + previousSpell);
|
||||||
previousSpell.undo();
|
previousSpell.undo();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void redoLastSpell() {
|
public void redoLastSpell() {
|
||||||
if (!redoStack.isEmpty()) {
|
if (!redoStack.isEmpty()) {
|
||||||
Command previousSpell = redoStack.pollLast();
|
Command previousSpell = redoStack.pollLast();
|
||||||
undoStack.offerLast(previousSpell);
|
undoStack.offerLast(previousSpell);
|
||||||
System.out.println(this + " redoes " + previousSpell);
|
System.out.println(this + " redoes " + previousSpell);
|
||||||
previousSpell.redo();
|
previousSpell.redo();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "Wizard";
|
return "Wizard";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,9 +11,9 @@ import com.iluwatar.command.App;
|
|||||||
*/
|
*/
|
||||||
public class AppTest {
|
public class AppTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void test() {
|
public void test() {
|
||||||
String[] args = {};
|
String[] args = {};
|
||||||
App.main(args);
|
App.main(args);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,33 +1,34 @@
|
|||||||
package com.iluwatar.composite;
|
package com.iluwatar.composite;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The Composite pattern is a partitioning design pattern. The Composite pattern
|
* The Composite pattern is a partitioning design pattern. The Composite pattern describes that a
|
||||||
* describes that a group of objects is to be treated in the same way as a single
|
* group of objects is to be treated in the same way as a single instance of an object. The intent
|
||||||
* instance of an object. The intent of a composite is to "compose" objects into
|
* of a composite is to "compose" objects into tree structures to represent part-whole hierarchies.
|
||||||
* tree structures to represent part-whole hierarchies. Implementing the Composite
|
* Implementing the Composite pattern lets clients treat individual objects and compositions
|
||||||
* pattern lets clients treat individual objects and compositions uniformly.
|
* uniformly.
|
||||||
* <p>
|
* <p>
|
||||||
* In this example we have sentences composed of words composed of letters. All of
|
* In this example we have sentences composed of words composed of letters. All of the objects can
|
||||||
* the objects can be treated through the same interface ({@link LetterComposite}).
|
* be treated through the same interface ({@link LetterComposite}).
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class App {
|
public class App {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Program entry point
|
* Program entry point
|
||||||
* @param args command line args
|
*
|
||||||
*/
|
* @param args command line args
|
||||||
public static void main(String[] args) {
|
*/
|
||||||
System.out.println("Message from the orcs: ");
|
public static void main(String[] args) {
|
||||||
|
System.out.println("Message from the orcs: ");
|
||||||
|
|
||||||
LetterComposite orcMessage = new Messenger().messageFromOrcs();
|
LetterComposite orcMessage = new Messenger().messageFromOrcs();
|
||||||
orcMessage.print();
|
orcMessage.print();
|
||||||
|
|
||||||
System.out.println("\n");
|
System.out.println("\n");
|
||||||
|
|
||||||
System.out.println("Message from the elves: ");
|
System.out.println("Message from the elves: ");
|
||||||
|
|
||||||
LetterComposite elfMessage = new Messenger().messageFromElves();
|
LetterComposite elfMessage = new Messenger().messageFromElves();
|
||||||
elfMessage.print();
|
elfMessage.print();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,20 +7,19 @@ package com.iluwatar.composite;
|
|||||||
*/
|
*/
|
||||||
public class Letter extends LetterComposite {
|
public class Letter extends LetterComposite {
|
||||||
|
|
||||||
private char c;
|
private char c;
|
||||||
|
|
||||||
public Letter(char c) {
|
public Letter(char c) {
|
||||||
this.c = c;
|
this.c = c;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void printThisBefore() {
|
protected void printThisBefore() {
|
||||||
System.out.print(c);
|
System.out.print(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void printThisAfter() {
|
|
||||||
// nop
|
|
||||||
}
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void printThisAfter() {
|
||||||
|
// nop
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,25 +10,25 @@ import java.util.List;
|
|||||||
*/
|
*/
|
||||||
public abstract class LetterComposite {
|
public abstract class LetterComposite {
|
||||||
|
|
||||||
private List<LetterComposite> children = new ArrayList<LetterComposite>();
|
private List<LetterComposite> children = new ArrayList<LetterComposite>();
|
||||||
|
|
||||||
public void add(LetterComposite letter) {
|
public void add(LetterComposite letter) {
|
||||||
children.add(letter);
|
children.add(letter);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int count() {
|
public int count() {
|
||||||
return children.size();
|
return children.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected abstract void printThisBefore();
|
protected abstract void printThisBefore();
|
||||||
|
|
||||||
protected abstract void printThisAfter();
|
protected abstract void printThisAfter();
|
||||||
|
|
||||||
public void print() {
|
public void print() {
|
||||||
printThisBefore();
|
printThisBefore();
|
||||||
for (LetterComposite letter : children) {
|
for (LetterComposite letter : children) {
|
||||||
letter.print();
|
letter.print();
|
||||||
}
|
}
|
||||||
printThisAfter();
|
printThisAfter();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,48 +11,47 @@ import java.util.List;
|
|||||||
*/
|
*/
|
||||||
public class Messenger {
|
public class Messenger {
|
||||||
|
|
||||||
LetterComposite messageFromOrcs() {
|
LetterComposite messageFromOrcs() {
|
||||||
|
|
||||||
List<Word> words = new ArrayList<Word>();
|
List<Word> words = new ArrayList<Word>();
|
||||||
|
|
||||||
words.add(new Word(Arrays.asList(new Letter('W'), new Letter('h'),
|
words.add(new Word(Arrays.asList(new Letter('W'), new Letter('h'), new Letter('e'), new Letter(
|
||||||
new Letter('e'), new Letter('r'), new Letter('e'))));
|
'r'), new Letter('e'))));
|
||||||
words.add(new Word(Arrays.asList(new Letter('t'), new Letter('h'),
|
words.add(new Word(Arrays.asList(new Letter('t'), new Letter('h'), new Letter('e'), new Letter(
|
||||||
new Letter('e'), new Letter('r'), new Letter('e'))));
|
'r'), new Letter('e'))));
|
||||||
words.add(new Word(Arrays.asList(new Letter('i'), new Letter('s'))));
|
words.add(new Word(Arrays.asList(new Letter('i'), new Letter('s'))));
|
||||||
words.add(new Word(Arrays.asList(new Letter('a'))));
|
words.add(new Word(Arrays.asList(new Letter('a'))));
|
||||||
words.add(new Word(Arrays.asList(new Letter('w'), new Letter('h'),
|
words.add(new Word(Arrays.asList(new Letter('w'), new Letter('h'), new Letter('i'), new Letter(
|
||||||
new Letter('i'), new Letter('p'))));
|
'p'))));
|
||||||
words.add(new Word(Arrays.asList(new Letter('t'), new Letter('h'),
|
words.add(new Word(Arrays.asList(new Letter('t'), new Letter('h'), new Letter('e'), new Letter(
|
||||||
new Letter('e'), new Letter('r'), new Letter('e'))));
|
'r'), new Letter('e'))));
|
||||||
words.add(new Word(Arrays.asList(new Letter('i'), new Letter('s'))));
|
words.add(new Word(Arrays.asList(new Letter('i'), new Letter('s'))));
|
||||||
words.add(new Word(Arrays.asList(new Letter('a'))));
|
words.add(new Word(Arrays.asList(new Letter('a'))));
|
||||||
words.add(new Word(Arrays.asList(new Letter('w'), new Letter('a'),
|
words.add(new Word(Arrays.asList(new Letter('w'), new Letter('a'), new Letter('y'))));
|
||||||
new Letter('y'))));
|
|
||||||
|
|
||||||
return new Sentence(words);
|
return new Sentence(words);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
LetterComposite messageFromElves() {
|
LetterComposite messageFromElves() {
|
||||||
|
|
||||||
List<Word> words = new ArrayList<Word>();
|
List<Word> words = new ArrayList<Word>();
|
||||||
|
|
||||||
words.add(new Word(Arrays.asList(new Letter('M'), new Letter('u'),
|
words.add(new Word(Arrays.asList(new Letter('M'), new Letter('u'), new Letter('c'), new Letter(
|
||||||
new Letter('c'), new Letter('h'))));
|
'h'))));
|
||||||
words.add(new Word(Arrays.asList(new Letter('w'), new Letter('i'),
|
words.add(new Word(Arrays.asList(new Letter('w'), new Letter('i'), new Letter('n'), new Letter(
|
||||||
new Letter('n'), new Letter('d'))));
|
'd'))));
|
||||||
words.add(new Word(Arrays.asList(new Letter('p'), new Letter('o'),
|
words.add(new Word(Arrays.asList(new Letter('p'), new Letter('o'), new Letter('u'), new Letter(
|
||||||
new Letter('u'), new Letter('r'), new Letter('s'))));
|
'r'), new Letter('s'))));
|
||||||
words.add(new Word(Arrays.asList(new Letter('f'), new Letter('r'),
|
words.add(new Word(Arrays.asList(new Letter('f'), new Letter('r'), new Letter('o'), new Letter(
|
||||||
new Letter('o'), new Letter('m'))));
|
'm'))));
|
||||||
words.add(new Word(Arrays.asList(new Letter('y'), new Letter('o'),
|
words.add(new Word(Arrays.asList(new Letter('y'), new Letter('o'), new Letter('u'), new Letter(
|
||||||
new Letter('u'), new Letter('r'))));
|
'r'))));
|
||||||
words.add(new Word(Arrays.asList(new Letter('m'), new Letter('o'),
|
words.add(new Word(Arrays.asList(new Letter('m'), new Letter('o'), new Letter('u'), new Letter(
|
||||||
new Letter('u'), new Letter('t'), new Letter('h'))));
|
't'), new Letter('h'))));
|
||||||
|
|
||||||
return new Sentence(words);
|
return new Sentence(words);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -9,20 +9,19 @@ import java.util.List;
|
|||||||
*/
|
*/
|
||||||
public class Sentence extends LetterComposite {
|
public class Sentence extends LetterComposite {
|
||||||
|
|
||||||
public Sentence(List<Word> words) {
|
public Sentence(List<Word> words) {
|
||||||
for (Word w : words) {
|
for (Word w : words) {
|
||||||
this.add(w);
|
this.add(w);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void printThisBefore() {
|
protected void printThisBefore() {
|
||||||
// nop
|
// nop
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void printThisAfter() {
|
|
||||||
System.out.print(".");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void printThisAfter() {
|
||||||
|
System.out.print(".");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,20 +9,19 @@ import java.util.List;
|
|||||||
*/
|
*/
|
||||||
public class Word extends LetterComposite {
|
public class Word extends LetterComposite {
|
||||||
|
|
||||||
public Word(List<Letter> letters) {
|
public Word(List<Letter> letters) {
|
||||||
for (Letter l : letters) {
|
for (Letter l : letters) {
|
||||||
this.add(l);
|
this.add(l);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void printThisBefore() {
|
protected void printThisBefore() {
|
||||||
System.out.print(" ");
|
System.out.print(" ");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void printThisAfter() {
|
|
||||||
// nop
|
|
||||||
}
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void printThisAfter() {
|
||||||
|
// nop
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,9 +11,9 @@ import com.iluwatar.composite.App;
|
|||||||
*/
|
*/
|
||||||
public class AppTest {
|
public class AppTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void test() {
|
public void test() {
|
||||||
String[] args = {};
|
String[] args = {};
|
||||||
App.main(args);
|
App.main(args);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,53 +7,55 @@ import org.apache.log4j.Logger;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Data Access Object (DAO) is an object that provides an abstract interface to some type of database or other
|
* Data Access Object (DAO) is an object that provides an abstract interface to some type of
|
||||||
* persistence mechanism. By mapping application calls to the persistence layer, DAO provide some specific data
|
* database or other persistence mechanism. By mapping application calls to the persistence layer,
|
||||||
* operations without exposing details of the database. This isolation supports the Single responsibility principle.
|
* DAO provide some specific data operations without exposing details of the database. This
|
||||||
* It separates what data accesses the application needs, in terms of domain-specific objects and data types
|
* isolation supports the Single responsibility principle. It separates what data accesses the
|
||||||
* (the public interface of the DAO), from how these needs can be satisfied with a specific DBMS.
|
* application needs, in terms of domain-specific objects and data types (the public interface of
|
||||||
|
* the DAO), from how these needs can be satisfied with a specific DBMS.
|
||||||
* <p>
|
* <p>
|
||||||
* With the DAO pattern, we can use various method calls to retrieve/add/delete/update data without directly
|
* With the DAO pattern, we can use various method calls to retrieve/add/delete/update data without
|
||||||
* interacting with the data. The below example demonstrates basic CRUD operations: select, add, update, and delete.
|
* directly interacting with the data. The below example demonstrates basic CRUD operations: select,
|
||||||
|
* add, update, and delete.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class App {
|
public class App {
|
||||||
|
|
||||||
private static Logger LOGGER = Logger.getLogger(App.class);
|
private static Logger LOGGER = Logger.getLogger(App.class);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Program entry point.
|
* Program entry point.
|
||||||
*
|
*
|
||||||
* @param args command line args.
|
* @param args command line args.
|
||||||
*/
|
*/
|
||||||
public static void main(final String[] args) {
|
public static void main(final String[] args) {
|
||||||
final CustomerDaoImpl customerDao = new CustomerDaoImpl(generateSampleCustomers());
|
final CustomerDaoImpl customerDao = new CustomerDaoImpl(generateSampleCustomers());
|
||||||
LOGGER.info("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
|
LOGGER.info("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
|
||||||
LOGGER.info("customerDao.getCusterById(2): " + customerDao.getCustomerById(2));
|
LOGGER.info("customerDao.getCusterById(2): " + customerDao.getCustomerById(2));
|
||||||
final Customer customer = new Customer(4, "Dan", "Danson");
|
final Customer customer = new Customer(4, "Dan", "Danson");
|
||||||
customerDao.addCustomer(customer);
|
customerDao.addCustomer(customer);
|
||||||
LOGGER.info("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
|
LOGGER.info("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
|
||||||
customer.setFirstName("Daniel");
|
customer.setFirstName("Daniel");
|
||||||
customer.setLastName("Danielson");
|
customer.setLastName("Danielson");
|
||||||
customerDao.updateCustomer(customer);
|
customerDao.updateCustomer(customer);
|
||||||
LOGGER.info("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
|
LOGGER.info("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
|
||||||
customerDao.deleteCustomer(customer);
|
customerDao.deleteCustomer(customer);
|
||||||
LOGGER.info("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
|
LOGGER.info("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generate customers.
|
* Generate customers.
|
||||||
*
|
*
|
||||||
* @return list of customers.
|
* @return list of customers.
|
||||||
*/
|
*/
|
||||||
public static List<Customer> generateSampleCustomers() {
|
public static List<Customer> generateSampleCustomers() {
|
||||||
final Customer customer1 = new Customer(1, "Adam", "Adamson");
|
final Customer customer1 = new Customer(1, "Adam", "Adamson");
|
||||||
final Customer customer2 = new Customer(2, "Bob", "Bobson");
|
final Customer customer2 = new Customer(2, "Bob", "Bobson");
|
||||||
final Customer customer3 = new Customer(3, "Carl", "Carlson");
|
final Customer customer3 = new Customer(3, "Carl", "Carlson");
|
||||||
final List<Customer> customers = new ArrayList<Customer>();
|
final List<Customer> customers = new ArrayList<Customer>();
|
||||||
customers.add(customer1);
|
customers.add(customer1);
|
||||||
customers.add(customer2);
|
customers.add(customer2);
|
||||||
customers.add(customer3);
|
customers.add(customer3);
|
||||||
return customers;
|
return customers;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,37 +2,37 @@ package com.iluwatar.decorator;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* The Decorator pattern is a more flexible alternative to subclassing. The Decorator
|
* The Decorator pattern is a more flexible alternative to subclassing. The Decorator class
|
||||||
* class implements the same interface as the target and uses composition to
|
* implements the same interface as the target and uses composition to "decorate" calls to the
|
||||||
* "decorate" calls to the target. Using the Decorator pattern it is possible to
|
* target. Using the Decorator pattern it is possible to change the behavior of the class during
|
||||||
* change the behavior of the class during runtime.
|
* runtime.
|
||||||
* <p>
|
* <p>
|
||||||
* In this example we show how the simple {@link Troll} first attacks and then
|
* In this example we show how the simple {@link Troll} first attacks and then flees the battle.
|
||||||
* flees the battle. Then we decorate the {@link Troll} with a {@link SmartTroll}
|
* Then we decorate the {@link Troll} with a {@link SmartTroll} and perform the attack again. You
|
||||||
* and perform the attack again. You can see how the behavior changes after the
|
* can see how the behavior changes after the decoration.
|
||||||
* decoration.
|
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class App {
|
public class App {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Program entry point
|
* Program entry point
|
||||||
* @param args command line args
|
*
|
||||||
*/
|
* @param args command line args
|
||||||
public static void main(String[] args) {
|
*/
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
// simple troll
|
// simple troll
|
||||||
System.out.println("A simple looking troll approaches.");
|
System.out.println("A simple looking troll approaches.");
|
||||||
Hostile troll = new Troll();
|
Hostile troll = new Troll();
|
||||||
troll.attack();
|
troll.attack();
|
||||||
troll.fleeBattle();
|
troll.fleeBattle();
|
||||||
System.out.printf("Simple troll power %d.\n", troll.getAttackPower());
|
System.out.printf("Simple troll power %d.\n", troll.getAttackPower());
|
||||||
|
|
||||||
// change the behavior of the simple troll by adding a decorator
|
// change the behavior of the simple troll by adding a decorator
|
||||||
System.out.println("\nA smart looking troll surprises you.");
|
System.out.println("\nA smart looking troll surprises you.");
|
||||||
Hostile smart = new SmartTroll(troll);
|
Hostile smart = new SmartTroll(troll);
|
||||||
smart.attack();
|
smart.attack();
|
||||||
smart.fleeBattle();
|
smart.fleeBattle();
|
||||||
System.out.printf("Smart troll power %d.\n", smart.getAttackPower());
|
System.out.printf("Smart troll power %d.\n", smart.getAttackPower());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,10 +7,10 @@ package com.iluwatar.decorator;
|
|||||||
*/
|
*/
|
||||||
public interface Hostile {
|
public interface Hostile {
|
||||||
|
|
||||||
void attack();
|
void attack();
|
||||||
|
|
||||||
int getAttackPower();
|
int getAttackPower();
|
||||||
|
|
||||||
void fleeBattle();
|
void fleeBattle();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,36 +1,34 @@
|
|||||||
package com.iluwatar.decorator;
|
package com.iluwatar.decorator;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* SmartTroll is a decorator for {@link Hostile} objects.
|
* SmartTroll is a decorator for {@link Hostile} objects. The calls to the {@link Hostile} interface
|
||||||
* The calls to the {@link Hostile} interface are intercepted
|
* are intercepted and decorated. Finally the calls are delegated to the decorated {@link Hostile}
|
||||||
* and decorated. Finally the calls are delegated
|
* object.
|
||||||
* to the decorated {@link Hostile} object.
|
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class SmartTroll implements Hostile {
|
public class SmartTroll implements Hostile {
|
||||||
|
|
||||||
private Hostile decorated;
|
private Hostile decorated;
|
||||||
|
|
||||||
public SmartTroll(Hostile decorated) {
|
public SmartTroll(Hostile decorated) {
|
||||||
this.decorated = decorated;
|
this.decorated = decorated;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void attack() {
|
public void attack() {
|
||||||
System.out.println("The troll throws a rock at you!");
|
System.out.println("The troll throws a rock at you!");
|
||||||
decorated.attack();
|
decorated.attack();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getAttackPower() {
|
public int getAttackPower() {
|
||||||
// decorated troll power + 20 because it is smart
|
// decorated troll power + 20 because it is smart
|
||||||
return decorated.getAttackPower() + 20;
|
return decorated.getAttackPower() + 20;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void fleeBattle() {
|
|
||||||
System.out.println("The troll calls for help!");
|
|
||||||
decorated.fleeBattle();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void fleeBattle() {
|
||||||
|
System.out.println("The troll calls for help!");
|
||||||
|
decorated.fleeBattle();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,17 +7,16 @@ package com.iluwatar.decorator;
|
|||||||
*/
|
*/
|
||||||
public class Troll implements Hostile {
|
public class Troll implements Hostile {
|
||||||
|
|
||||||
public void attack() {
|
public void attack() {
|
||||||
System.out.println("The troll swings at you with a club!");
|
System.out.println("The troll swings at you with a club!");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getAttackPower() {
|
public int getAttackPower() {
|
||||||
return 10;
|
return 10;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void fleeBattle() {
|
|
||||||
System.out.println("The troll shrieks in horror and runs away!");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
public void fleeBattle() {
|
||||||
|
System.out.println("The troll shrieks in horror and runs away!");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,9 +11,9 @@ import com.iluwatar.decorator.App;
|
|||||||
*/
|
*/
|
||||||
public class AppTest {
|
public class AppTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void test() {
|
public void test() {
|
||||||
String[] args = {};
|
String[] args = {};
|
||||||
App.main(args);
|
App.main(args);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,21 +2,20 @@ package com.iluwatar.dependency.injection;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* AdvancedWizard implements inversion of control.
|
* AdvancedWizard implements inversion of control. It depends on abstraction that can be injected
|
||||||
* It depends on abstraction that can be injected through
|
* through its constructor.
|
||||||
* its constructor.
|
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class AdvancedWizard implements Wizard {
|
public class AdvancedWizard implements Wizard {
|
||||||
|
|
||||||
private Tobacco tobacco;
|
|
||||||
|
|
||||||
public AdvancedWizard(Tobacco tobacco) {
|
private Tobacco tobacco;
|
||||||
this.tobacco = tobacco;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
public AdvancedWizard(Tobacco tobacco) {
|
||||||
public void smoke() {
|
this.tobacco = tobacco;
|
||||||
tobacco.smoke(this);
|
}
|
||||||
}
|
|
||||||
|
@Override
|
||||||
|
public void smoke() {
|
||||||
|
tobacco.smoke(this);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,40 +5,41 @@ import com.google.inject.Injector;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Dependency Injection pattern deals with how objects handle their dependencies. The pattern
|
* Dependency Injection pattern deals with how objects handle their dependencies. The pattern
|
||||||
* implements so called inversion of control principle. Inversion of control has two specific rules:
|
* implements so called inversion of control principle. Inversion of control has two specific rules:
|
||||||
* - High-level modules should not depend on low-level modules. Both should depend on abstractions.
|
* - High-level modules should not depend on low-level modules. Both should depend on abstractions.
|
||||||
* - Abstractions should not depend on details. Details should depend on abstractions.
|
* - Abstractions should not depend on details. Details should depend on abstractions.
|
||||||
* <p>
|
* <p>
|
||||||
* In this example we show you three different wizards. The first one ({@link SimpleWizard}) is a naive
|
* In this example we show you three different wizards. The first one ({@link SimpleWizard}) is a
|
||||||
* implementation violating the inversion of control principle. It depends directly on a concrete
|
* naive implementation violating the inversion of control principle. It depends directly on a
|
||||||
* implementation which cannot be changed.
|
* concrete implementation which cannot be changed.
|
||||||
* <p>
|
* <p>
|
||||||
* The second wizard ({@link AdvancedWizard}) is more flexible. It does not depend on any concrete implementation
|
* The second wizard ({@link AdvancedWizard}) is more flexible. It does not depend on any concrete
|
||||||
* but abstraction. It utilizes Dependency Injection pattern allowing its {@link Tobacco} dependency to be
|
* implementation but abstraction. It utilizes Dependency Injection pattern allowing its
|
||||||
* injected through its constructor. This way, handling the dependency is no longer the wizard's
|
* {@link Tobacco} dependency to be injected through its constructor. This way, handling the
|
||||||
* responsibility. It is resolved outside the wizard class.
|
* dependency is no longer the wizard's responsibility. It is resolved outside the wizard class.
|
||||||
* <p>
|
* <p>
|
||||||
* The third example takes the pattern a step further. It uses Guice framework for Dependency Injection.
|
* The third example takes the pattern a step further. It uses Guice framework for Dependency
|
||||||
* {@link TobaccoModule} binds a concrete implementation to abstraction. Injector is then used to create
|
* Injection. {@link TobaccoModule} binds a concrete implementation to abstraction. Injector is then
|
||||||
* {@link GuiceWizard} object with correct dependencies.
|
* used to create {@link GuiceWizard} object with correct dependencies.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class App {
|
public class App {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Program entry point
|
* Program entry point
|
||||||
* @param args command line args
|
*
|
||||||
*/
|
* @param args command line args
|
||||||
public static void main( String[] args ) {
|
*/
|
||||||
SimpleWizard simpleWizard = new SimpleWizard();
|
public static void main(String[] args) {
|
||||||
simpleWizard.smoke();
|
SimpleWizard simpleWizard = new SimpleWizard();
|
||||||
|
simpleWizard.smoke();
|
||||||
AdvancedWizard advancedWizard = new AdvancedWizard(new SecondBreakfastTobacco());
|
|
||||||
advancedWizard.smoke();
|
AdvancedWizard advancedWizard = new AdvancedWizard(new SecondBreakfastTobacco());
|
||||||
|
advancedWizard.smoke();
|
||||||
Injector injector = Guice.createInjector(new TobaccoModule());
|
|
||||||
GuiceWizard guiceWizard = injector.getInstance(GuiceWizard.class);
|
Injector injector = Guice.createInjector(new TobaccoModule());
|
||||||
guiceWizard.smoke();
|
GuiceWizard guiceWizard = injector.getInstance(GuiceWizard.class);
|
||||||
}
|
guiceWizard.smoke();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,22 +4,21 @@ import javax.inject.Inject;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* GuiceWizard implements inversion of control.
|
* GuiceWizard implements inversion of control. Its dependencies are injected through its
|
||||||
* Its dependencies are injected through its constructor
|
* constructor by Guice framework.
|
||||||
* by Guice framework.
|
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class GuiceWizard implements Wizard {
|
public class GuiceWizard implements Wizard {
|
||||||
|
|
||||||
private Tobacco tobacco;
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
public GuiceWizard(Tobacco tobacco) {
|
|
||||||
this.tobacco = tobacco;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
private Tobacco tobacco;
|
||||||
public void smoke() {
|
|
||||||
tobacco.smoke(this);
|
@Inject
|
||||||
}
|
public GuiceWizard(Tobacco tobacco) {
|
||||||
|
this.tobacco = tobacco;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void smoke() {
|
||||||
|
tobacco.smoke(this);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,15 +2,15 @@ package com.iluwatar.dependency.injection;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Naive Wizard implementation violating the inversion of control principle.
|
* Naive Wizard implementation violating the inversion of control principle. It should depend on
|
||||||
* It should depend on abstraction instead.
|
* abstraction instead.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class SimpleWizard implements Wizard {
|
public class SimpleWizard implements Wizard {
|
||||||
|
|
||||||
private OldTobyTobacco tobacco = new OldTobyTobacco();
|
private OldTobyTobacco tobacco = new OldTobyTobacco();
|
||||||
|
|
||||||
public void smoke() {
|
public void smoke() {
|
||||||
tobacco.smoke(this);
|
tobacco.smoke(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,8 +6,9 @@ package com.iluwatar.dependency.injection;
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public abstract class Tobacco {
|
public abstract class Tobacco {
|
||||||
|
|
||||||
public void smoke(Wizard wizard) {
|
public void smoke(Wizard wizard) {
|
||||||
System.out.println(String.format("%s smoking %s", wizard.getClass().getSimpleName(), this.getClass().getSimpleName()));
|
System.out.println(String.format("%s smoking %s", wizard.getClass().getSimpleName(), this
|
||||||
}
|
.getClass().getSimpleName()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,8 +9,8 @@ import com.google.inject.AbstractModule;
|
|||||||
*/
|
*/
|
||||||
public class TobaccoModule extends AbstractModule {
|
public class TobaccoModule extends AbstractModule {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void configure() {
|
protected void configure() {
|
||||||
bind(Tobacco.class).to(RivendellTobacco.class);
|
bind(Tobacco.class).to(RivendellTobacco.class);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@ package com.iluwatar.dependency.injection;
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public interface Wizard {
|
public interface Wizard {
|
||||||
|
|
||||||
void smoke();
|
void smoke();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -11,9 +11,9 @@ import com.iluwatar.dependency.injection.App;
|
|||||||
*/
|
*/
|
||||||
public class AppTest {
|
public class AppTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void test() {
|
public void test() {
|
||||||
String[] args = {};
|
String[] args = {};
|
||||||
App.main(args);
|
App.main(args);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user