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,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.
* <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.
*
*
*/
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();
}
}

View File

@ -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();
}

View File

@ -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";
}
}

View File

@ -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";
}
}

View File

@ -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";
}
}

View File

@ -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;
}
}

View File

@ -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();
}
}

View File

@ -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;
}
}

View File

@ -10,38 +10,37 @@ import java.util.LinkedList;
*/
public class Wizard {
private Deque<Command> undoStack = new LinkedList<>();
private Deque<Command> redoStack = new LinkedList<>();
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);
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";
}
}

View File

@ -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);
}
}