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

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

View File

@ -2,23 +2,25 @@ 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.
* <p>
* 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.
* <p>
* 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) {

View File

@ -2,8 +2,9 @@ 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 {

View File

@ -11,5 +11,4 @@ public class SimpleTask extends Task {
public void execute() {
System.out.println("Perform some important activity and after call the callback method.");
}
}

View File

@ -5,8 +5,8 @@ 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.
*/

View File

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

View File

@ -2,21 +2,22 @@ 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.
* <p>
* 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.
* <p>
* 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.
*
*
*/
@ -24,6 +25,7 @@ public class App {
/**
* Program entry point
*
* @param args command line args
*/
public static void main(String[] args) {

View File

@ -31,8 +31,8 @@ public abstract class Target {
public abstract String toString();
public void printStatus() {
System.out.println(String.format("%s, [size=%s] [visibility=%s]", this,
getSize(), getVisibility()));
System.out.println(String.format("%s, [size=%s] [visibility=%s]", this, getSize(),
getVisibility()));
System.out.println();
}
}

View File

@ -13,8 +13,7 @@ public class Wizard {
private Deque<Command> undoStack = new LinkedList<>();
private Deque<Command> redoStack = new LinkedList<>();
public Wizard() {
}
public Wizard() {}
public void castSpell(Command command, Target target) {
System.out.println(this + " casts " + command + " at " + target);

View File

@ -1,20 +1,21 @@
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.
* <p>
* 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) {

View File

@ -22,5 +22,4 @@ public class Letter extends LetterComposite {
protected void printThisAfter() {
// nop
}
}

View File

@ -15,20 +15,19 @@ public class Messenger {
List<Word> words = new ArrayList<Word>();
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('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('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('a'), new Letter('y'))));
return new Sentence(words);
@ -38,18 +37,18 @@ public class Messenger {
List<Word> words = new ArrayList<Word>();
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);

View File

@ -24,5 +24,4 @@ public class Sentence extends LetterComposite {
protected void printThisAfter() {
System.out.print(".");
}
}

View File

@ -24,5 +24,4 @@ public class Word extends LetterComposite {
protected void printThisAfter() {
// nop
}
}

View File

@ -7,14 +7,16 @@ 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.
* <p>
* 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 {

View File

@ -2,21 +2,21 @@ 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.
* <p>
* 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) {

View File

@ -1,10 +1,9 @@
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 {
@ -32,5 +31,4 @@ public class SmartTroll implements Hostile {
System.out.println("The troll calls for help!");
decorated.fleeBattle();
}
}

View File

@ -19,5 +19,4 @@ public class Troll implements Hostile {
public void fleeBattle() {
System.out.println("The troll shrieks in horror and runs away!");
}
}

View File

@ -2,9 +2,8 @@ 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 {

View File

@ -10,27 +10,28 @@ import com.google.inject.Injector;
* - 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.
* <p>
* 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.
* 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.
* <p>
* 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 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.
* <p>
* 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.
* 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 ) {
public static void main(String[] args) {
SimpleWizard simpleWizard = new SimpleWizard();
simpleWizard.smoke();

View File

@ -4,9 +4,8 @@ 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 {

View File

@ -2,8 +2,8 @@ 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 {

View File

@ -8,6 +8,7 @@ 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()));
System.out.println(String.format("%s smoking %s", wizard.getClass().getSimpleName(), this
.getClass().getSimpleName()));
}
}