diff --git a/business-delegate/src/main/java/com/iluwatar/business/delegate/App.java b/business-delegate/src/main/java/com/iluwatar/business/delegate/App.java index eea7608eb..c900f7366 100644 --- a/business-delegate/src/main/java/com/iluwatar/business/delegate/App.java +++ b/business-delegate/src/main/java/com/iluwatar/business/delegate/App.java @@ -2,34 +2,36 @@ package com.iluwatar.business.delegate; /** * - * The Business Delegate pattern adds an abstraction layer between the presentation and business tiers. - * By using the pattern we gain loose coupling between the tiers. The Business Delegate encapsulates - * knowledge about how to locate, connect to, and interact with the business objects that make up - * the application. + * The Business Delegate pattern adds an abstraction layer between the presentation and business + * tiers. By using the pattern we gain loose coupling between the tiers. The Business Delegate + * encapsulates knowledge about how to locate, connect to, and interact with the business objects + * that make up the application. *

- * Some of the services the Business Delegate uses are instantiated directly, and some can be retrieved - * through service lookups. The Business Delegate itself may contain business logic too potentially tying - * together multiple service calls, exception handling, retrying etc. + * Some of the services the Business Delegate uses are instantiated directly, and some can be + * retrieved through service lookups. The Business Delegate itself may contain business logic too + * potentially tying together multiple service calls, exception handling, retrying etc. *

- * In this example the client ({@link Client}) utilizes a business delegate ({@link BusinessDelegate}) to execute a task. - * The Business Delegate then selects the appropriate service and makes the service call. + * In this example the client ({@link Client}) utilizes a business delegate ( + * {@link BusinessDelegate}) to execute a task. The Business Delegate then selects the appropriate + * service and makes the service call. * */ 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); - client.doTask(); - } + BusinessDelegate businessDelegate = new BusinessDelegate(); + businessDelegate.setServiceType(ServiceType.EJB); + + Client client = new Client(businessDelegate); + client.doTask(); + + businessDelegate.setServiceType(ServiceType.JMS); + client.doTask(); + } } diff --git a/business-delegate/src/main/java/com/iluwatar/business/delegate/BusinessDelegate.java b/business-delegate/src/main/java/com/iluwatar/business/delegate/BusinessDelegate.java index cf0809b97..b8a70aa0e 100644 --- a/business-delegate/src/main/java/com/iluwatar/business/delegate/BusinessDelegate.java +++ b/business-delegate/src/main/java/com/iluwatar/business/delegate/BusinessDelegate.java @@ -6,17 +6,17 @@ package com.iluwatar.business.delegate; * */ public class BusinessDelegate { - - private BusinessLookup lookupService = new BusinessLookup(); - private BusinessService businessService; - private ServiceType serviceType; - public void setServiceType(ServiceType serviceType) { - this.serviceType = serviceType; - } + private BusinessLookup lookupService = new BusinessLookup(); + private BusinessService businessService; + private ServiceType serviceType; - public void doTask() { - businessService = lookupService.getBusinessService(serviceType); - businessService.doProcessing(); - } + public void setServiceType(ServiceType serviceType) { + this.serviceType = serviceType; + } + + public void doTask() { + businessService = lookupService.getBusinessService(serviceType); + businessService.doProcessing(); + } } diff --git a/business-delegate/src/main/java/com/iluwatar/business/delegate/BusinessLookup.java b/business-delegate/src/main/java/com/iluwatar/business/delegate/BusinessLookup.java index 6a5f2d504..7cea16580 100644 --- a/business-delegate/src/main/java/com/iluwatar/business/delegate/BusinessLookup.java +++ b/business-delegate/src/main/java/com/iluwatar/business/delegate/BusinessLookup.java @@ -7,11 +7,11 @@ package com.iluwatar.business.delegate; */ public class BusinessLookup { - public BusinessService getBusinessService(ServiceType serviceType) { - if (serviceType.equals(ServiceType.EJB)) { - return new EjbService(); - } else { - return new JmsService(); - } - } + public BusinessService getBusinessService(ServiceType serviceType) { + if (serviceType.equals(ServiceType.EJB)) { + return new EjbService(); + } else { + return new JmsService(); + } + } } diff --git a/business-delegate/src/main/java/com/iluwatar/business/delegate/BusinessService.java b/business-delegate/src/main/java/com/iluwatar/business/delegate/BusinessService.java index 7e39745d5..dfeaf883a 100644 --- a/business-delegate/src/main/java/com/iluwatar/business/delegate/BusinessService.java +++ b/business-delegate/src/main/java/com/iluwatar/business/delegate/BusinessService.java @@ -7,5 +7,5 @@ package com.iluwatar.business.delegate; */ public interface BusinessService { - void doProcessing(); + void doProcessing(); } diff --git a/business-delegate/src/main/java/com/iluwatar/business/delegate/Client.java b/business-delegate/src/main/java/com/iluwatar/business/delegate/Client.java index 2dc0cc662..eab295cf2 100644 --- a/business-delegate/src/main/java/com/iluwatar/business/delegate/Client.java +++ b/business-delegate/src/main/java/com/iluwatar/business/delegate/Client.java @@ -7,13 +7,13 @@ package com.iluwatar.business.delegate; */ public class Client { - private BusinessDelegate businessDelegate; + private BusinessDelegate businessDelegate; - public Client(BusinessDelegate businessDelegate) { - this.businessDelegate = businessDelegate; - } + public Client(BusinessDelegate businessDelegate) { + this.businessDelegate = businessDelegate; + } - public void doTask() { - businessDelegate.doTask(); - } + public void doTask() { + businessDelegate.doTask(); + } } diff --git a/business-delegate/src/main/java/com/iluwatar/business/delegate/EjbService.java b/business-delegate/src/main/java/com/iluwatar/business/delegate/EjbService.java index bd03db45d..f387449e2 100644 --- a/business-delegate/src/main/java/com/iluwatar/business/delegate/EjbService.java +++ b/business-delegate/src/main/java/com/iluwatar/business/delegate/EjbService.java @@ -7,8 +7,8 @@ package com.iluwatar.business.delegate; */ public class EjbService implements BusinessService { - @Override - public void doProcessing() { - System.out.println("EjbService is now processing"); - } + @Override + public void doProcessing() { + System.out.println("EjbService is now processing"); + } } diff --git a/business-delegate/src/main/java/com/iluwatar/business/delegate/JmsService.java b/business-delegate/src/main/java/com/iluwatar/business/delegate/JmsService.java index 37425755a..fd74cf3c2 100644 --- a/business-delegate/src/main/java/com/iluwatar/business/delegate/JmsService.java +++ b/business-delegate/src/main/java/com/iluwatar/business/delegate/JmsService.java @@ -7,8 +7,8 @@ package com.iluwatar.business.delegate; */ public class JmsService implements BusinessService { - @Override - public void doProcessing() { - System.out.println("JmsService is now processing"); - } + @Override + public void doProcessing() { + System.out.println("JmsService is now processing"); + } } diff --git a/business-delegate/src/main/java/com/iluwatar/business/delegate/ServiceType.java b/business-delegate/src/main/java/com/iluwatar/business/delegate/ServiceType.java index e26d71ae6..ac42d3b6c 100644 --- a/business-delegate/src/main/java/com/iluwatar/business/delegate/ServiceType.java +++ b/business-delegate/src/main/java/com/iluwatar/business/delegate/ServiceType.java @@ -6,6 +6,6 @@ package com.iluwatar.business.delegate; * */ public enum ServiceType { - - EJB, JMS; + + EJB, JMS; } diff --git a/business-delegate/src/test/java/com/iluwatar/business/delegate/AppTest.java b/business-delegate/src/test/java/com/iluwatar/business/delegate/AppTest.java index 7ce63c2b4..5ff7e6784 100644 --- a/business-delegate/src/test/java/com/iluwatar/business/delegate/AppTest.java +++ b/business-delegate/src/test/java/com/iluwatar/business/delegate/AppTest.java @@ -10,10 +10,10 @@ import com.iluwatar.business.delegate.App; * */ public class AppTest { - - @Test - public void test() { - String[] args = {}; - App.main(args); - } + + @Test + public void test() { + String[] args = {}; + App.main(args); + } } diff --git a/callback/src/main/java/com/iluwatar/callback/App.java b/callback/src/main/java/com/iluwatar/callback/App.java index 513a32415..81cb16f73 100644 --- a/callback/src/main/java/com/iluwatar/callback/App.java +++ b/callback/src/main/java/com/iluwatar/callback/App.java @@ -2,20 +2,21 @@ package com.iluwatar.callback; /** * - * Callback pattern is more native for functional languages where functions are treated as first-class citizens. - * Prior to Java 8 callbacks can be simulated using simple (alike command) interfaces. + * Callback pattern is more native for functional languages where functions are treated as + * first-class citizens. Prior to Java 8 callbacks can be simulated using simple (alike command) + * interfaces. * */ public class App { - public static void main(String[] args) { - Task task = new SimpleTask(); - Callback callback = new Callback() { - @Override - public void call() { - System.out.println("I'm done now."); - } - }; - task.executeWith(callback); - } + public static void main(String[] args) { + Task task = new SimpleTask(); + Callback callback = new Callback() { + @Override + public void call() { + System.out.println("I'm done now."); + } + }; + task.executeWith(callback); + } } diff --git a/callback/src/main/java/com/iluwatar/callback/Callback.java b/callback/src/main/java/com/iluwatar/callback/Callback.java index 81a421c0d..08939298b 100644 --- a/callback/src/main/java/com/iluwatar/callback/Callback.java +++ b/callback/src/main/java/com/iluwatar/callback/Callback.java @@ -7,5 +7,5 @@ package com.iluwatar.callback; */ public interface Callback { - public void call(); + public void call(); } diff --git a/callback/src/main/java/com/iluwatar/callback/SimpleTask.java b/callback/src/main/java/com/iluwatar/callback/SimpleTask.java index 70b844ce3..a651ed7b6 100644 --- a/callback/src/main/java/com/iluwatar/callback/SimpleTask.java +++ b/callback/src/main/java/com/iluwatar/callback/SimpleTask.java @@ -7,9 +7,8 @@ package com.iluwatar.callback; */ public class SimpleTask extends Task { - @Override - public void execute() { - System.out.println("Perform some important activity and after call the callback method."); - } - + @Override + public void execute() { + System.out.println("Perform some important activity and after call the callback method."); + } } diff --git a/callback/src/main/java/com/iluwatar/callback/Task.java b/callback/src/main/java/com/iluwatar/callback/Task.java index db4b66dc5..d3be6c7a0 100644 --- a/callback/src/main/java/com/iluwatar/callback/Task.java +++ b/callback/src/main/java/com/iluwatar/callback/Task.java @@ -7,12 +7,12 @@ package com.iluwatar.callback; */ public abstract class Task { - public final void executeWith(Callback callback) { - execute(); - if (callback != null) { - callback.call(); - } - } + public final void executeWith(Callback callback) { + execute(); + if (callback != null) { + callback.call(); + } + } - public abstract void execute(); + public abstract void execute(); } diff --git a/callback/src/test/java/com/iluwatar/callback/AppTest.java b/callback/src/test/java/com/iluwatar/callback/AppTest.java index 0f7a6f45e..67046a175 100644 --- a/callback/src/test/java/com/iluwatar/callback/AppTest.java +++ b/callback/src/test/java/com/iluwatar/callback/AppTest.java @@ -5,35 +5,35 @@ import org.junit.Test; import static org.junit.Assert.assertEquals; /** - * Add a field as a counter. Every time the callback method is called increment this - * field. Unit test checks that the field is being incremented. + * Add a field as a counter. Every time the callback method is called increment this field. Unit + * test checks that the field is being incremented. * * Could be done with mock objects as well where the call method call is verified. */ public class AppTest { - private Integer callingCount = 0; + private Integer callingCount = 0; - @Test - public void test() { - Callback callback = new Callback() { - @Override - public void call() { - callingCount++; - } - }; + @Test + public void test() { + Callback callback = new Callback() { + @Override + public void call() { + 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); - } + } } diff --git a/chain/src/main/java/com/iluwatar/chain/App.java b/chain/src/main/java/com/iluwatar/chain/App.java index 4d3ca69db..ae8c74143 100644 --- a/chain/src/main/java/com/iluwatar/chain/App.java +++ b/chain/src/main/java/com/iluwatar/chain/App.java @@ -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. *

- * 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")); - } + } } diff --git a/chain/src/main/java/com/iluwatar/chain/OrcCommander.java b/chain/src/main/java/com/iluwatar/chain/OrcCommander.java index fb5134e01..73de1b8b2 100644 --- a/chain/src/main/java/com/iluwatar/chain/OrcCommander.java +++ b/chain/src/main/java/com/iluwatar/chain/OrcCommander.java @@ -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"; + } } diff --git a/chain/src/main/java/com/iluwatar/chain/OrcKing.java b/chain/src/main/java/com/iluwatar/chain/OrcKing.java index b39959935..640b190b2 100644 --- a/chain/src/main/java/com/iluwatar/chain/OrcKing.java +++ b/chain/src/main/java/com/iluwatar/chain/OrcKing.java @@ -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); + } } diff --git a/chain/src/main/java/com/iluwatar/chain/OrcOfficer.java b/chain/src/main/java/com/iluwatar/chain/OrcOfficer.java index 131cb1101..68df3ec6f 100644 --- a/chain/src/main/java/com/iluwatar/chain/OrcOfficer.java +++ b/chain/src/main/java/com/iluwatar/chain/OrcOfficer.java @@ -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"; + } } diff --git a/chain/src/main/java/com/iluwatar/chain/OrcSoldier.java b/chain/src/main/java/com/iluwatar/chain/OrcSoldier.java index a681dfb77..d96f51702 100644 --- a/chain/src/main/java/com/iluwatar/chain/OrcSoldier.java +++ b/chain/src/main/java/com/iluwatar/chain/OrcSoldier.java @@ -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"; + } } diff --git a/chain/src/main/java/com/iluwatar/chain/Request.java b/chain/src/main/java/com/iluwatar/chain/Request.java index 558ee65d1..0c62cfd43 100644 --- a/chain/src/main/java/com/iluwatar/chain/Request.java +++ b/chain/src/main/java/com/iluwatar/chain/Request.java @@ -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(); + } } diff --git a/chain/src/main/java/com/iluwatar/chain/RequestHandler.java b/chain/src/main/java/com/iluwatar/chain/RequestHandler.java index 5570c20ce..fd58b9ea8 100644 --- a/chain/src/main/java/com/iluwatar/chain/RequestHandler.java +++ b/chain/src/main/java/com/iluwatar/chain/RequestHandler.java @@ -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(); } diff --git a/chain/src/main/java/com/iluwatar/chain/RequestType.java b/chain/src/main/java/com/iluwatar/chain/RequestType.java index 9ad975d2f..443d6dced 100644 --- a/chain/src/main/java/com/iluwatar/chain/RequestType.java +++ b/chain/src/main/java/com/iluwatar/chain/RequestType.java @@ -7,6 +7,6 @@ package com.iluwatar.chain; */ public enum RequestType { - DEFEND_CASTLE, TORTURE_PRISONER, COLLECT_TAX + DEFEND_CASTLE, TORTURE_PRISONER, COLLECT_TAX } diff --git a/chain/src/test/java/com/iluwatar/chain/AppTest.java b/chain/src/test/java/com/iluwatar/chain/AppTest.java index aa52e60e2..bd28b007a 100644 --- a/chain/src/test/java/com/iluwatar/chain/AppTest.java +++ b/chain/src/test/java/com/iluwatar/chain/AppTest.java @@ -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); + } } diff --git a/command/src/main/java/com/iluwatar/command/App.java b/command/src/main/java/com/iluwatar/command/App.java index b421b683b..423ce6075 100644 --- a/command/src/main/java/com/iluwatar/command/App.java +++ b/command/src/main/java/com/iluwatar/command/App.java @@ -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 - * needed to perform an action or trigger an event at a later time. This information includes the method name, - * the object that owns the method and values for the method parameters. + * The Command pattern is a behavioral design pattern in which an object is used to encapsulate all + * information needed to perform an action or trigger an event at a later time. This information + * includes the method name, the object that owns the method and values for the method parameters. *

- * Four terms always associated with the command pattern are command, receiver, invoker and client. A command - * object (spell) knows about the receiver (target) and invokes a method of the receiver. Values for parameters of - * the receiver method are stored in the command. The receiver then does the work. An invoker object (wizard) - * knows how to execute a command, and optionally does bookkeeping about the command execution. The invoker - * does not know anything about a concrete command, it knows only about command interface. Both an invoker object - * and several command 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. + * Four terms always associated with the command pattern are command, receiver, invoker and client. + * A command object (spell) knows about the receiver (target) and invokes a method of the receiver. + * Values for parameters of the receiver method are stored in the command. The receiver then does + * the work. An invoker object (wizard) knows how to execute a command, and optionally does + * bookkeeping about the command execution. The invoker does not know anything about a concrete + * command, it knows only about command interface. Both an invoker object and several command + * 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. *

- * In other words, in this example the wizard casts spells on the goblin. The wizard keeps track of the previous - * spells cast, so it is easy to undo them. In addition, the wizard keeps track of the spells undone, so they - * can be redone. + * In other words, in this example the wizard casts spells on the goblin. The wizard keeps track of + * the previous spells cast, so it is easy to undo them. In addition, the wizard keeps track of the + * spells undone, so they can be redone. * * */ public class App { - /** - * Program entry point - * @param args command line args - */ - public static void main(String[] args) { - Wizard wizard = new Wizard(); - Goblin goblin = new Goblin(); + /** + * Program entry point + * + * @param args command line args + */ + public static void main(String[] args) { + Wizard wizard = new Wizard(); + Goblin goblin = new Goblin(); - goblin.printStatus(); + goblin.printStatus(); - wizard.castSpell(new ShrinkSpell(), goblin); - goblin.printStatus(); + wizard.castSpell(new ShrinkSpell(), goblin); + goblin.printStatus(); - wizard.castSpell(new InvisibilitySpell(), goblin); - goblin.printStatus(); + wizard.castSpell(new InvisibilitySpell(), goblin); + goblin.printStatus(); - wizard.undoLastSpell(); - goblin.printStatus(); + wizard.undoLastSpell(); + goblin.printStatus(); - wizard.undoLastSpell(); - goblin.printStatus(); + wizard.undoLastSpell(); + goblin.printStatus(); - wizard.redoLastSpell(); - goblin.printStatus(); + wizard.redoLastSpell(); + goblin.printStatus(); - wizard.redoLastSpell(); - goblin.printStatus(); - } + wizard.redoLastSpell(); + goblin.printStatus(); + } } diff --git a/command/src/main/java/com/iluwatar/command/Command.java b/command/src/main/java/com/iluwatar/command/Command.java index 9ffeed24d..4b5127263 100644 --- a/command/src/main/java/com/iluwatar/command/Command.java +++ b/command/src/main/java/com/iluwatar/command/Command.java @@ -7,13 +7,13 @@ package com.iluwatar.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 - public abstract String toString(); + @Override + public abstract String toString(); } diff --git a/command/src/main/java/com/iluwatar/command/Goblin.java b/command/src/main/java/com/iluwatar/command/Goblin.java index 7d0804b0c..d5fcb7078 100644 --- a/command/src/main/java/com/iluwatar/command/Goblin.java +++ b/command/src/main/java/com/iluwatar/command/Goblin.java @@ -7,14 +7,14 @@ package com.iluwatar.command; */ public class Goblin extends Target { - public Goblin() { - setSize(Size.NORMAL); - setVisibility(Visibility.VISIBLE); - } + public Goblin() { + setSize(Size.NORMAL); + setVisibility(Visibility.VISIBLE); + } - @Override - public String toString() { - return "Goblin"; - } + @Override + public String toString() { + return "Goblin"; + } } diff --git a/command/src/main/java/com/iluwatar/command/InvisibilitySpell.java b/command/src/main/java/com/iluwatar/command/InvisibilitySpell.java index b72c34cc2..02f1b1cc1 100644 --- a/command/src/main/java/com/iluwatar/command/InvisibilitySpell.java +++ b/command/src/main/java/com/iluwatar/command/InvisibilitySpell.java @@ -7,30 +7,30 @@ package com.iluwatar.command; */ public class InvisibilitySpell extends Command { - private Target target; + private Target target; - @Override - public void execute(Target target) { - target.setVisibility(Visibility.INVISIBLE); - this.target = target; - } + @Override + public void execute(Target target) { + target.setVisibility(Visibility.INVISIBLE); + this.target = target; + } - @Override - public void undo() { - if (target != null) { - target.setVisibility(Visibility.VISIBLE); - } - } + @Override + public void undo() { + if (target != null) { + target.setVisibility(Visibility.VISIBLE); + } + } - @Override - public void redo() { - if (target != null) { - target.setVisibility(Visibility.INVISIBLE); - } - } + @Override + public void redo() { + if (target != null) { + target.setVisibility(Visibility.INVISIBLE); + } + } - @Override - public String toString() { - return "Invisibility spell"; - } + @Override + public String toString() { + return "Invisibility spell"; + } } diff --git a/command/src/main/java/com/iluwatar/command/ShrinkSpell.java b/command/src/main/java/com/iluwatar/command/ShrinkSpell.java index f36438082..46448bf6c 100644 --- a/command/src/main/java/com/iluwatar/command/ShrinkSpell.java +++ b/command/src/main/java/com/iluwatar/command/ShrinkSpell.java @@ -7,32 +7,32 @@ package com.iluwatar.command; */ public class ShrinkSpell extends Command { - private Size oldSize; - private Target target; + private Size oldSize; + private Target target; - @Override - public void execute(Target target) { - oldSize = target.getSize(); - target.setSize(Size.SMALL); - this.target = target; - } + @Override + public void execute(Target target) { + oldSize = target.getSize(); + target.setSize(Size.SMALL); + this.target = target; + } - @Override - public void undo() { - if (oldSize != null && target != null) { - Size temp = target.getSize(); - target.setSize(oldSize); - oldSize = temp; - } - } + @Override + public void undo() { + if (oldSize != null && target != null) { + Size temp = target.getSize(); + target.setSize(oldSize); + oldSize = temp; + } + } - @Override - public void redo() { - undo(); - } + @Override + public void redo() { + undo(); + } - @Override - public String toString() { - return "Shrink spell"; - } + @Override + public String toString() { + return "Shrink spell"; + } } diff --git a/command/src/main/java/com/iluwatar/command/Size.java b/command/src/main/java/com/iluwatar/command/Size.java index a9c20bd59..2a443b449 100644 --- a/command/src/main/java/com/iluwatar/command/Size.java +++ b/command/src/main/java/com/iluwatar/command/Size.java @@ -7,16 +7,16 @@ package com.iluwatar.command; */ public enum Size { - SMALL("small"), NORMAL("normal"), LARGE("large"), UNDEFINED(""); - - private String title; + SMALL("small"), NORMAL("normal"), LARGE("large"), UNDEFINED(""); - Size(String title) { - this.title = title; - } + private String title; - @Override - public String toString() { - return title; - } + Size(String title) { + this.title = title; + } + + @Override + public String toString() { + return title; + } } diff --git a/command/src/main/java/com/iluwatar/command/Target.java b/command/src/main/java/com/iluwatar/command/Target.java index 6ea2681d3..e12f758ff 100644 --- a/command/src/main/java/com/iluwatar/command/Target.java +++ b/command/src/main/java/com/iluwatar/command/Target.java @@ -7,32 +7,32 @@ package com.iluwatar.command; */ public abstract class Target { - private Size size; + private Size size; - private Visibility visibility; + private Visibility visibility; - public Size getSize() { - return size; - } + public Size getSize() { + return size; + } - public void setSize(Size size) { - this.size = size; - } + public void setSize(Size size) { + this.size = size; + } - public Visibility getVisibility() { - return visibility; - } + public Visibility getVisibility() { + return visibility; + } - public void setVisibility(Visibility visibility) { - this.visibility = visibility; - } + public void setVisibility(Visibility visibility) { + this.visibility = visibility; + } - @Override - public abstract String toString(); + @Override + public abstract String toString(); - public void printStatus() { - System.out.println(String.format("%s, [size=%s] [visibility=%s]", this, - getSize(), getVisibility())); - System.out.println(); - } + public void printStatus() { + System.out.println(String.format("%s, [size=%s] [visibility=%s]", this, getSize(), + getVisibility())); + System.out.println(); + } } diff --git a/command/src/main/java/com/iluwatar/command/Visibility.java b/command/src/main/java/com/iluwatar/command/Visibility.java index 3316ac9e3..ebf8a306c 100644 --- a/command/src/main/java/com/iluwatar/command/Visibility.java +++ b/command/src/main/java/com/iluwatar/command/Visibility.java @@ -7,16 +7,16 @@ package com.iluwatar.command; */ public enum Visibility { - VISIBLE("visible"), INVISIBLE("invisible"), UNDEFINED(""); + VISIBLE("visible"), INVISIBLE("invisible"), UNDEFINED(""); - private String title; + private String title; - Visibility(String title) { - this.title = title; - } + Visibility(String title) { + this.title = title; + } - @Override - public String toString() { - return title; - } + @Override + public String toString() { + return title; + } } diff --git a/command/src/main/java/com/iluwatar/command/Wizard.java b/command/src/main/java/com/iluwatar/command/Wizard.java index 995b4441a..edef8d3a9 100644 --- a/command/src/main/java/com/iluwatar/command/Wizard.java +++ b/command/src/main/java/com/iluwatar/command/Wizard.java @@ -10,38 +10,37 @@ import java.util.LinkedList; */ public class Wizard { - private Deque undoStack = new LinkedList<>(); - private Deque redoStack = new LinkedList<>(); + private Deque undoStack = new LinkedList<>(); + private Deque redoStack = new LinkedList<>(); - public Wizard() { - } + public Wizard() {} - public void castSpell(Command command, Target target) { - System.out.println(this + " casts " + command + " at " + target); - command.execute(target); - undoStack.offerLast(command); - } + public void castSpell(Command command, Target target) { + System.out.println(this + " casts " + command + " at " + target); + command.execute(target); + undoStack.offerLast(command); + } - public void undoLastSpell() { - if (!undoStack.isEmpty()) { - Command previousSpell = undoStack.pollLast(); - redoStack.offerLast(previousSpell); - System.out.println(this + " undoes " + previousSpell); - previousSpell.undo(); - } - } + public void undoLastSpell() { + if (!undoStack.isEmpty()) { + Command previousSpell = undoStack.pollLast(); + redoStack.offerLast(previousSpell); + System.out.println(this + " undoes " + previousSpell); + previousSpell.undo(); + } + } - public void redoLastSpell() { - if (!redoStack.isEmpty()) { - Command previousSpell = redoStack.pollLast(); - undoStack.offerLast(previousSpell); - System.out.println(this + " redoes " + previousSpell); - previousSpell.redo(); - } - } + public void redoLastSpell() { + if (!redoStack.isEmpty()) { + Command previousSpell = redoStack.pollLast(); + undoStack.offerLast(previousSpell); + System.out.println(this + " redoes " + previousSpell); + previousSpell.redo(); + } + } - @Override - public String toString() { - return "Wizard"; - } + @Override + public String toString() { + return "Wizard"; + } } diff --git a/command/src/test/java/com/iluwatar/command/AppTest.java b/command/src/test/java/com/iluwatar/command/AppTest.java index 2fd5c16b4..aa0af3571 100644 --- a/command/src/test/java/com/iluwatar/command/AppTest.java +++ b/command/src/test/java/com/iluwatar/command/AppTest.java @@ -11,9 +11,9 @@ import com.iluwatar.command.App; */ public class AppTest { - @Test - public void test() { - String[] args = {}; - App.main(args); - } + @Test + public void test() { + String[] args = {}; + App.main(args); + } } diff --git a/composite/src/main/java/com/iluwatar/composite/App.java b/composite/src/main/java/com/iluwatar/composite/App.java index 7bd0e4d01..57207cb8f 100644 --- a/composite/src/main/java/com/iluwatar/composite/App.java +++ b/composite/src/main/java/com/iluwatar/composite/App.java @@ -1,33 +1,34 @@ package com.iluwatar.composite; /** - * The Composite pattern is a partitioning design pattern. The Composite pattern - * describes that a group of objects is to be treated in the same way as a single - * instance of an object. The intent of a composite is to "compose" objects into - * tree structures to represent part-whole hierarchies. Implementing the Composite - * pattern lets clients treat individual objects and compositions uniformly. + * The Composite pattern is a partitioning design pattern. The Composite pattern describes that a + * group of objects is to be treated in the same way as a single instance of an object. The intent + * of a composite is to "compose" objects into tree structures to represent part-whole hierarchies. + * Implementing the Composite pattern lets clients treat individual objects and compositions + * uniformly. *

- * In this example we have sentences composed of words composed of letters. All of - * the objects can be treated through the same interface ({@link LetterComposite}). + * In this example we have sentences composed of words composed of letters. All of the objects can + * be treated through the same interface ({@link LetterComposite}). * */ public class App { - /** - * Program entry point - * @param args command line args - */ - public static void main(String[] args) { - System.out.println("Message from the orcs: "); + /** + * Program entry point + * + * @param args command line args + */ + public static void main(String[] args) { + System.out.println("Message from the orcs: "); - LetterComposite orcMessage = new Messenger().messageFromOrcs(); - orcMessage.print(); + LetterComposite orcMessage = new Messenger().messageFromOrcs(); + 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(); - elfMessage.print(); - } + LetterComposite elfMessage = new Messenger().messageFromElves(); + elfMessage.print(); + } } diff --git a/composite/src/main/java/com/iluwatar/composite/Letter.java b/composite/src/main/java/com/iluwatar/composite/Letter.java index 8304ea801..4071eecda 100644 --- a/composite/src/main/java/com/iluwatar/composite/Letter.java +++ b/composite/src/main/java/com/iluwatar/composite/Letter.java @@ -7,20 +7,19 @@ package com.iluwatar.composite; */ public class Letter extends LetterComposite { - private char c; + private char c; - public Letter(char c) { - this.c = c; - } + public Letter(char c) { + this.c = c; + } - @Override - protected void printThisBefore() { - System.out.print(c); - } - - @Override - protected void printThisAfter() { - // nop - } + @Override + protected void printThisBefore() { + System.out.print(c); + } + @Override + protected void printThisAfter() { + // nop + } } diff --git a/composite/src/main/java/com/iluwatar/composite/LetterComposite.java b/composite/src/main/java/com/iluwatar/composite/LetterComposite.java index e58d51b25..1fdf4fdb6 100644 --- a/composite/src/main/java/com/iluwatar/composite/LetterComposite.java +++ b/composite/src/main/java/com/iluwatar/composite/LetterComposite.java @@ -10,25 +10,25 @@ import java.util.List; */ public abstract class LetterComposite { - private List children = new ArrayList(); + private List children = new ArrayList(); - public void add(LetterComposite letter) { - children.add(letter); - } + public void add(LetterComposite letter) { + children.add(letter); + } - public int count() { - return children.size(); - } + public int count() { + return children.size(); + } - protected abstract void printThisBefore(); + protected abstract void printThisBefore(); - protected abstract void printThisAfter(); + protected abstract void printThisAfter(); - public void print() { - printThisBefore(); - for (LetterComposite letter : children) { - letter.print(); - } - printThisAfter(); - } + public void print() { + printThisBefore(); + for (LetterComposite letter : children) { + letter.print(); + } + printThisAfter(); + } } diff --git a/composite/src/main/java/com/iluwatar/composite/Messenger.java b/composite/src/main/java/com/iluwatar/composite/Messenger.java index aa0560d4d..37fa84bc2 100644 --- a/composite/src/main/java/com/iluwatar/composite/Messenger.java +++ b/composite/src/main/java/com/iluwatar/composite/Messenger.java @@ -11,48 +11,47 @@ import java.util.List; */ public class Messenger { - LetterComposite messageFromOrcs() { + LetterComposite messageFromOrcs() { - List words = new ArrayList(); + List words = new ArrayList(); - words.add(new Word(Arrays.asList(new Letter('W'), new Letter('h'), - new Letter('e'), new Letter('r'), new Letter('e')))); - words.add(new Word(Arrays.asList(new Letter('t'), new Letter('h'), - new Letter('e'), new Letter('r'), new Letter('e')))); - 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('w'), new Letter('h'), - new Letter('i'), new Letter('p')))); - words.add(new Word(Arrays.asList(new Letter('t'), new Letter('h'), - new Letter('e'), new Letter('r'), new Letter('e')))); - 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('w'), new Letter('a'), - new Letter('y')))); + words.add(new Word(Arrays.asList(new Letter('W'), new Letter('h'), new Letter('e'), new Letter( + 'r'), new Letter('e')))); + words.add(new Word(Arrays.asList(new Letter('t'), new Letter('h'), new Letter('e'), new Letter( + 'r'), new Letter('e')))); + 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('w'), new Letter('h'), new Letter('i'), new Letter( + 'p')))); + words.add(new Word(Arrays.asList(new Letter('t'), new Letter('h'), new Letter('e'), new Letter( + 'r'), new Letter('e')))); + 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('w'), new Letter('a'), new Letter('y')))); - return new Sentence(words); + return new Sentence(words); - } + } - LetterComposite messageFromElves() { + LetterComposite messageFromElves() { - List words = new ArrayList(); + List words = new ArrayList(); - words.add(new Word(Arrays.asList(new Letter('M'), new Letter('u'), - new Letter('c'), new Letter('h')))); - words.add(new Word(Arrays.asList(new Letter('w'), new Letter('i'), - new Letter('n'), new Letter('d')))); - words.add(new Word(Arrays.asList(new Letter('p'), new Letter('o'), - new Letter('u'), new Letter('r'), new Letter('s')))); - words.add(new Word(Arrays.asList(new Letter('f'), new Letter('r'), - new Letter('o'), new Letter('m')))); - words.add(new Word(Arrays.asList(new Letter('y'), new Letter('o'), - new Letter('u'), new Letter('r')))); - words.add(new Word(Arrays.asList(new Letter('m'), new Letter('o'), - new Letter('u'), new Letter('t'), new Letter('h')))); + words.add(new Word(Arrays.asList(new Letter('M'), new Letter('u'), new Letter('c'), new Letter( + 'h')))); + words.add(new Word(Arrays.asList(new Letter('w'), new Letter('i'), new Letter('n'), new Letter( + 'd')))); + words.add(new Word(Arrays.asList(new Letter('p'), new Letter('o'), new Letter('u'), new Letter( + 'r'), new Letter('s')))); + words.add(new Word(Arrays.asList(new Letter('f'), new Letter('r'), new Letter('o'), new Letter( + 'm')))); + words.add(new Word(Arrays.asList(new Letter('y'), new Letter('o'), new Letter('u'), new Letter( + 'r')))); + words.add(new Word(Arrays.asList(new Letter('m'), new Letter('o'), new Letter('u'), new Letter( + 't'), new Letter('h')))); - return new Sentence(words); + return new Sentence(words); - } + } } diff --git a/composite/src/main/java/com/iluwatar/composite/Sentence.java b/composite/src/main/java/com/iluwatar/composite/Sentence.java index 2fc13701b..e6c626ea2 100644 --- a/composite/src/main/java/com/iluwatar/composite/Sentence.java +++ b/composite/src/main/java/com/iluwatar/composite/Sentence.java @@ -9,20 +9,19 @@ import java.util.List; */ public class Sentence extends LetterComposite { - public Sentence(List words) { - for (Word w : words) { - this.add(w); - } - } + public Sentence(List words) { + for (Word w : words) { + this.add(w); + } + } - @Override - protected void printThisBefore() { - // nop - } - - @Override - protected void printThisAfter() { - System.out.print("."); - } + @Override + protected void printThisBefore() { + // nop + } + @Override + protected void printThisAfter() { + System.out.print("."); + } } diff --git a/composite/src/main/java/com/iluwatar/composite/Word.java b/composite/src/main/java/com/iluwatar/composite/Word.java index e715ed28a..3060b0a1b 100644 --- a/composite/src/main/java/com/iluwatar/composite/Word.java +++ b/composite/src/main/java/com/iluwatar/composite/Word.java @@ -9,20 +9,19 @@ import java.util.List; */ public class Word extends LetterComposite { - public Word(List letters) { - for (Letter l : letters) { - this.add(l); - } - } + public Word(List letters) { + for (Letter l : letters) { + this.add(l); + } + } - @Override - protected void printThisBefore() { - System.out.print(" "); - } - - @Override - protected void printThisAfter() { - // nop - } + @Override + protected void printThisBefore() { + System.out.print(" "); + } + @Override + protected void printThisAfter() { + // nop + } } diff --git a/composite/src/test/java/com/iluwatar/composite/AppTest.java b/composite/src/test/java/com/iluwatar/composite/AppTest.java index 872ccea78..574e8def4 100644 --- a/composite/src/test/java/com/iluwatar/composite/AppTest.java +++ b/composite/src/test/java/com/iluwatar/composite/AppTest.java @@ -11,9 +11,9 @@ import com.iluwatar.composite.App; */ public class AppTest { - @Test - public void test() { - String[] args = {}; - App.main(args); - } + @Test + public void test() { + String[] args = {}; + App.main(args); + } } diff --git a/dao/src/main/java/com/iluwatar/dao/App.java b/dao/src/main/java/com/iluwatar/dao/App.java index 95cf93e5b..2e115d8ce 100644 --- a/dao/src/main/java/com/iluwatar/dao/App.java +++ b/dao/src/main/java/com/iluwatar/dao/App.java @@ -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 - * persistence mechanism. By mapping application calls to the persistence layer, DAO provide some specific data - * operations without exposing details of the database. This isolation supports the Single responsibility principle. - * It separates what data accesses the 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. + * Data Access Object (DAO) is an object that provides an abstract interface to some type of + * database or other persistence mechanism. By mapping application calls to the persistence layer, + * DAO provide some specific data operations without exposing details of the database. This + * isolation supports the Single responsibility principle. It separates what data accesses the + * 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. *

- * With the DAO pattern, we can use various method calls to retrieve/add/delete/update data without directly - * interacting with the data. The below example demonstrates basic CRUD operations: select, add, update, and delete. + * With the DAO pattern, we can use various method calls to retrieve/add/delete/update data without + * directly interacting with the data. The below example demonstrates basic CRUD operations: select, + * add, update, and delete. * */ public class App { - private static Logger LOGGER = Logger.getLogger(App.class); + private static Logger LOGGER = Logger.getLogger(App.class); - /** - * Program entry point. - * - * @param args command line args. - */ - public static void main(final String[] args) { - final CustomerDaoImpl customerDao = new CustomerDaoImpl(generateSampleCustomers()); - LOGGER.info("customerDao.getAllCustomers(): " + customerDao.getAllCustomers()); - LOGGER.info("customerDao.getCusterById(2): " + customerDao.getCustomerById(2)); - final Customer customer = new Customer(4, "Dan", "Danson"); - customerDao.addCustomer(customer); - LOGGER.info("customerDao.getAllCustomers(): " + customerDao.getAllCustomers()); - customer.setFirstName("Daniel"); - customer.setLastName("Danielson"); - customerDao.updateCustomer(customer); - LOGGER.info("customerDao.getAllCustomers(): " + customerDao.getAllCustomers()); - customerDao.deleteCustomer(customer); - LOGGER.info("customerDao.getAllCustomers(): " + customerDao.getAllCustomers()); - } + /** + * Program entry point. + * + * @param args command line args. + */ + public static void main(final String[] args) { + final CustomerDaoImpl customerDao = new CustomerDaoImpl(generateSampleCustomers()); + LOGGER.info("customerDao.getAllCustomers(): " + customerDao.getAllCustomers()); + LOGGER.info("customerDao.getCusterById(2): " + customerDao.getCustomerById(2)); + final Customer customer = new Customer(4, "Dan", "Danson"); + customerDao.addCustomer(customer); + LOGGER.info("customerDao.getAllCustomers(): " + customerDao.getAllCustomers()); + customer.setFirstName("Daniel"); + customer.setLastName("Danielson"); + customerDao.updateCustomer(customer); + LOGGER.info("customerDao.getAllCustomers(): " + customerDao.getAllCustomers()); + customerDao.deleteCustomer(customer); + LOGGER.info("customerDao.getAllCustomers(): " + customerDao.getAllCustomers()); + } - /** - * Generate customers. - * - * @return list of customers. - */ - public static List generateSampleCustomers() { - final Customer customer1 = new Customer(1, "Adam", "Adamson"); - final Customer customer2 = new Customer(2, "Bob", "Bobson"); - final Customer customer3 = new Customer(3, "Carl", "Carlson"); - final List customers = new ArrayList(); - customers.add(customer1); - customers.add(customer2); - customers.add(customer3); - return customers; - } + /** + * Generate customers. + * + * @return list of customers. + */ + public static List generateSampleCustomers() { + final Customer customer1 = new Customer(1, "Adam", "Adamson"); + final Customer customer2 = new Customer(2, "Bob", "Bobson"); + final Customer customer3 = new Customer(3, "Carl", "Carlson"); + final List customers = new ArrayList(); + customers.add(customer1); + customers.add(customer2); + customers.add(customer3); + return customers; + } } diff --git a/decorator/src/main/java/com/iluwatar/decorator/App.java b/decorator/src/main/java/com/iluwatar/decorator/App.java index bd697d4a4..d58d3b61a 100644 --- a/decorator/src/main/java/com/iluwatar/decorator/App.java +++ b/decorator/src/main/java/com/iluwatar/decorator/App.java @@ -2,37 +2,37 @@ package com.iluwatar.decorator; /** * - * The Decorator pattern is a more flexible alternative to subclassing. The Decorator - * class implements the same interface as the target and uses composition to - * "decorate" calls to the target. Using the Decorator pattern it is possible to - * change the behavior of the class during runtime. + * The Decorator pattern is a more flexible alternative to subclassing. The Decorator class + * implements the same interface as the target and uses composition to "decorate" calls to the + * target. Using the Decorator pattern it is possible to change the behavior of the class during + * runtime. *

- * In this example we show how the simple {@link Troll} first attacks and then - * flees the battle. Then we decorate the {@link Troll} with a {@link SmartTroll} - * and perform the attack again. You can see how the behavior changes after the - * decoration. + * In this example we show how the simple {@link Troll} first attacks and then flees the battle. + * Then we decorate the {@link Troll} with a {@link SmartTroll} and perform the attack again. You + * can see how the behavior changes after the decoration. * */ 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) { - // simple troll - System.out.println("A simple looking troll approaches."); - Hostile troll = new Troll(); - troll.attack(); - troll.fleeBattle(); - System.out.printf("Simple troll power %d.\n", troll.getAttackPower()); + // simple troll + System.out.println("A simple looking troll approaches."); + Hostile troll = new Troll(); + troll.attack(); + troll.fleeBattle(); + System.out.printf("Simple troll power %d.\n", troll.getAttackPower()); - // change the behavior of the simple troll by adding a decorator - System.out.println("\nA smart looking troll surprises you."); - Hostile smart = new SmartTroll(troll); - smart.attack(); - smart.fleeBattle(); - System.out.printf("Smart troll power %d.\n", smart.getAttackPower()); - } + // change the behavior of the simple troll by adding a decorator + System.out.println("\nA smart looking troll surprises you."); + Hostile smart = new SmartTroll(troll); + smart.attack(); + smart.fleeBattle(); + System.out.printf("Smart troll power %d.\n", smart.getAttackPower()); + } } diff --git a/decorator/src/main/java/com/iluwatar/decorator/Hostile.java b/decorator/src/main/java/com/iluwatar/decorator/Hostile.java index 709072501..8b8f0c255 100644 --- a/decorator/src/main/java/com/iluwatar/decorator/Hostile.java +++ b/decorator/src/main/java/com/iluwatar/decorator/Hostile.java @@ -7,10 +7,10 @@ package com.iluwatar.decorator; */ public interface Hostile { - void attack(); + void attack(); - int getAttackPower(); + int getAttackPower(); - void fleeBattle(); + void fleeBattle(); } diff --git a/decorator/src/main/java/com/iluwatar/decorator/SmartTroll.java b/decorator/src/main/java/com/iluwatar/decorator/SmartTroll.java index 909f94c95..93927237d 100644 --- a/decorator/src/main/java/com/iluwatar/decorator/SmartTroll.java +++ b/decorator/src/main/java/com/iluwatar/decorator/SmartTroll.java @@ -1,36 +1,34 @@ package com.iluwatar.decorator; /** - * SmartTroll is a decorator for {@link Hostile} objects. - * The calls to the {@link Hostile} interface are intercepted - * and decorated. Finally the calls are delegated - * to the decorated {@link Hostile} object. + * SmartTroll is a decorator for {@link Hostile} objects. The calls to the {@link Hostile} interface + * are intercepted and decorated. Finally the calls are delegated to the decorated {@link Hostile} + * object. * */ public class SmartTroll implements Hostile { - private Hostile decorated; + private Hostile decorated; - public SmartTroll(Hostile decorated) { - this.decorated = decorated; - } + public SmartTroll(Hostile decorated) { + this.decorated = decorated; + } - @Override - public void attack() { - System.out.println("The troll throws a rock at you!"); - decorated.attack(); - } + @Override + public void attack() { + System.out.println("The troll throws a rock at you!"); + decorated.attack(); + } - @Override - public int getAttackPower() { - // decorated troll power + 20 because it is smart - return decorated.getAttackPower() + 20; - } - - @Override - public void fleeBattle() { - System.out.println("The troll calls for help!"); - decorated.fleeBattle(); - } + @Override + public int getAttackPower() { + // decorated troll power + 20 because it is smart + return decorated.getAttackPower() + 20; + } + @Override + public void fleeBattle() { + System.out.println("The troll calls for help!"); + decorated.fleeBattle(); + } } diff --git a/decorator/src/main/java/com/iluwatar/decorator/Troll.java b/decorator/src/main/java/com/iluwatar/decorator/Troll.java index 85d873dbe..a10f76f79 100644 --- a/decorator/src/main/java/com/iluwatar/decorator/Troll.java +++ b/decorator/src/main/java/com/iluwatar/decorator/Troll.java @@ -7,17 +7,16 @@ package com.iluwatar.decorator; */ public class Troll implements Hostile { - public void attack() { - System.out.println("The troll swings at you with a club!"); - } + public void attack() { + System.out.println("The troll swings at you with a club!"); + } - @Override - public int getAttackPower() { - return 10; - } - - public void fleeBattle() { - System.out.println("The troll shrieks in horror and runs away!"); - } + @Override + public int getAttackPower() { + return 10; + } + public void fleeBattle() { + System.out.println("The troll shrieks in horror and runs away!"); + } } diff --git a/decorator/src/test/java/com/iluwatar/decorator/AppTest.java b/decorator/src/test/java/com/iluwatar/decorator/AppTest.java index b74bd3a06..f6fa96092 100644 --- a/decorator/src/test/java/com/iluwatar/decorator/AppTest.java +++ b/decorator/src/test/java/com/iluwatar/decorator/AppTest.java @@ -11,9 +11,9 @@ import com.iluwatar.decorator.App; */ public class AppTest { - @Test - public void test() { - String[] args = {}; - App.main(args); - } + @Test + public void test() { + String[] args = {}; + App.main(args); + } } diff --git a/dependency-injection/src/main/java/com/iluwatar/dependency/injection/AdvancedWizard.java b/dependency-injection/src/main/java/com/iluwatar/dependency/injection/AdvancedWizard.java index 8202cc58e..810957858 100644 --- a/dependency-injection/src/main/java/com/iluwatar/dependency/injection/AdvancedWizard.java +++ b/dependency-injection/src/main/java/com/iluwatar/dependency/injection/AdvancedWizard.java @@ -2,21 +2,20 @@ package com.iluwatar.dependency.injection; /** * - * AdvancedWizard implements inversion of control. - * It depends on abstraction that can be injected through - * its constructor. + * AdvancedWizard implements inversion of control. It depends on abstraction that can be injected + * through its constructor. * */ public class AdvancedWizard implements Wizard { - - private Tobacco tobacco; - public AdvancedWizard(Tobacco tobacco) { - this.tobacco = tobacco; - } + private Tobacco tobacco; - @Override - public void smoke() { - tobacco.smoke(this); - } + public AdvancedWizard(Tobacco tobacco) { + this.tobacco = tobacco; + } + + @Override + public void smoke() { + tobacco.smoke(this); + } } diff --git a/dependency-injection/src/main/java/com/iluwatar/dependency/injection/App.java b/dependency-injection/src/main/java/com/iluwatar/dependency/injection/App.java index a882863b7..0205724b5 100644 --- a/dependency-injection/src/main/java/com/iluwatar/dependency/injection/App.java +++ b/dependency-injection/src/main/java/com/iluwatar/dependency/injection/App.java @@ -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: - * - 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. - *

- * In this example we show you three different wizards. The first one ({@link SimpleWizard}) is a naive - * implementation violating the inversion of control principle. It depends directly on a concrete - * implementation which cannot be changed. - *

- * The second wizard ({@link AdvancedWizard}) is more flexible. It does not depend on any concrete implementation - * but abstraction. It utilizes Dependency Injection pattern allowing its {@link Tobacco} dependency to be - * injected through its constructor. This way, handling the dependency is no longer the wizard's - * responsibility. It is resolved outside the wizard class. - *

- * The third example takes the pattern a step further. It uses Guice framework for Dependency Injection. - * {@link TobaccoModule} binds a concrete implementation to abstraction. Injector is then used to create - * {@link GuiceWizard} object with correct dependencies. + * - 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. + *

+ * In this example we show you three different wizards. The first one ({@link SimpleWizard}) is a + * naive implementation violating the inversion of control principle. It depends directly on a + * concrete implementation which cannot be changed. + *

+ * The second wizard ({@link AdvancedWizard}) is more flexible. It does not depend on any concrete + * implementation but abstraction. It utilizes Dependency Injection pattern allowing its + * {@link Tobacco} dependency to be injected through its constructor. This way, handling the + * dependency is no longer the wizard's responsibility. It is resolved outside the wizard class. + *

+ * The third example takes the pattern a step further. It uses Guice framework for Dependency + * Injection. {@link TobaccoModule} binds a concrete implementation to abstraction. Injector is then + * used to create {@link GuiceWizard} object with correct dependencies. * */ public class App { - - /** - * Program entry point - * @param args command line args - */ - public static void main( String[] args ) { - SimpleWizard simpleWizard = new SimpleWizard(); - simpleWizard.smoke(); - - AdvancedWizard advancedWizard = new AdvancedWizard(new SecondBreakfastTobacco()); - advancedWizard.smoke(); - - Injector injector = Guice.createInjector(new TobaccoModule()); - GuiceWizard guiceWizard = injector.getInstance(GuiceWizard.class); - guiceWizard.smoke(); - } + + /** + * Program entry point + * + * @param args command line args + */ + public static void main(String[] args) { + SimpleWizard simpleWizard = new SimpleWizard(); + simpleWizard.smoke(); + + AdvancedWizard advancedWizard = new AdvancedWizard(new SecondBreakfastTobacco()); + advancedWizard.smoke(); + + Injector injector = Guice.createInjector(new TobaccoModule()); + GuiceWizard guiceWizard = injector.getInstance(GuiceWizard.class); + guiceWizard.smoke(); + } } diff --git a/dependency-injection/src/main/java/com/iluwatar/dependency/injection/GuiceWizard.java b/dependency-injection/src/main/java/com/iluwatar/dependency/injection/GuiceWizard.java index 9393377f1..e5c77ba18 100644 --- a/dependency-injection/src/main/java/com/iluwatar/dependency/injection/GuiceWizard.java +++ b/dependency-injection/src/main/java/com/iluwatar/dependency/injection/GuiceWizard.java @@ -4,22 +4,21 @@ import javax.inject.Inject; /** * - * GuiceWizard implements inversion of control. - * Its dependencies are injected through its constructor - * by Guice framework. + * GuiceWizard implements inversion of control. Its dependencies are injected through its + * constructor by Guice framework. * */ public class GuiceWizard implements Wizard { - - private Tobacco tobacco; - - @Inject - public GuiceWizard(Tobacco tobacco) { - this.tobacco = tobacco; - } - @Override - public void smoke() { - tobacco.smoke(this); - } + private Tobacco tobacco; + + @Inject + public GuiceWizard(Tobacco tobacco) { + this.tobacco = tobacco; + } + + @Override + public void smoke() { + tobacco.smoke(this); + } } diff --git a/dependency-injection/src/main/java/com/iluwatar/dependency/injection/SimpleWizard.java b/dependency-injection/src/main/java/com/iluwatar/dependency/injection/SimpleWizard.java index 5bc2c8377..976616e74 100644 --- a/dependency-injection/src/main/java/com/iluwatar/dependency/injection/SimpleWizard.java +++ b/dependency-injection/src/main/java/com/iluwatar/dependency/injection/SimpleWizard.java @@ -2,15 +2,15 @@ package com.iluwatar.dependency.injection; /** * - * Naive Wizard implementation violating the inversion of control principle. - * It should depend on abstraction instead. + * Naive Wizard implementation violating the inversion of control principle. It should depend on + * abstraction instead. * */ public class SimpleWizard implements Wizard { - - private OldTobyTobacco tobacco = new OldTobyTobacco(); - - public void smoke() { - tobacco.smoke(this); - } + + private OldTobyTobacco tobacco = new OldTobyTobacco(); + + public void smoke() { + tobacco.smoke(this); + } } diff --git a/dependency-injection/src/main/java/com/iluwatar/dependency/injection/Tobacco.java b/dependency-injection/src/main/java/com/iluwatar/dependency/injection/Tobacco.java index 7ee97404d..48e4cd8de 100644 --- a/dependency-injection/src/main/java/com/iluwatar/dependency/injection/Tobacco.java +++ b/dependency-injection/src/main/java/com/iluwatar/dependency/injection/Tobacco.java @@ -6,8 +6,9 @@ package com.iluwatar.dependency.injection; * */ public abstract class Tobacco { - - public void smoke(Wizard wizard) { - System.out.println(String.format("%s smoking %s", wizard.getClass().getSimpleName(), this.getClass().getSimpleName())); - } + + public void smoke(Wizard wizard) { + System.out.println(String.format("%s smoking %s", wizard.getClass().getSimpleName(), this + .getClass().getSimpleName())); + } } diff --git a/dependency-injection/src/main/java/com/iluwatar/dependency/injection/TobaccoModule.java b/dependency-injection/src/main/java/com/iluwatar/dependency/injection/TobaccoModule.java index d2dd1072e..8187bae9f 100644 --- a/dependency-injection/src/main/java/com/iluwatar/dependency/injection/TobaccoModule.java +++ b/dependency-injection/src/main/java/com/iluwatar/dependency/injection/TobaccoModule.java @@ -9,8 +9,8 @@ import com.google.inject.AbstractModule; */ public class TobaccoModule extends AbstractModule { - @Override - protected void configure() { - bind(Tobacco.class).to(RivendellTobacco.class); - } + @Override + protected void configure() { + bind(Tobacco.class).to(RivendellTobacco.class); + } } diff --git a/dependency-injection/src/main/java/com/iluwatar/dependency/injection/Wizard.java b/dependency-injection/src/main/java/com/iluwatar/dependency/injection/Wizard.java index 8ac9f9fcb..0376fcc2e 100644 --- a/dependency-injection/src/main/java/com/iluwatar/dependency/injection/Wizard.java +++ b/dependency-injection/src/main/java/com/iluwatar/dependency/injection/Wizard.java @@ -6,7 +6,7 @@ package com.iluwatar.dependency.injection; * */ public interface Wizard { - - void smoke(); + + void smoke(); } diff --git a/dependency-injection/src/test/java/com/iluwatar/dependency/injection/AppTest.java b/dependency-injection/src/test/java/com/iluwatar/dependency/injection/AppTest.java index 5315fe1db..8d6411028 100644 --- a/dependency-injection/src/test/java/com/iluwatar/dependency/injection/AppTest.java +++ b/dependency-injection/src/test/java/com/iluwatar/dependency/injection/AppTest.java @@ -11,9 +11,9 @@ import com.iluwatar.dependency.injection.App; */ public class AppTest { - @Test - public void test() { - String[] args = {}; - App.main(args); - } + @Test + public void test() { + String[] args = {}; + App.main(args); + } }