Changed package naming across all examples.
This commit is contained in:
36
command/src/main/java/com/iluwatar/command/App.java
Normal file
36
command/src/main/java/com/iluwatar/command/App.java
Normal file
@@ -0,0 +1,36 @@
|
||||
package com.iluwatar.command;
|
||||
|
||||
/**
|
||||
*
|
||||
* In Command pattern actions are objects that can be executed and undone.
|
||||
*
|
||||
* In this example the commands are the spells cast by the wizard on the goblin.
|
||||
*
|
||||
*/
|
||||
public class App {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Wizard wizard = new Wizard();
|
||||
Goblin goblin = new Goblin();
|
||||
|
||||
goblin.printStatus();
|
||||
|
||||
wizard.castSpell(new ShrinkSpell(), goblin);
|
||||
goblin.printStatus();
|
||||
|
||||
wizard.castSpell(new InvisibilitySpell(), goblin);
|
||||
goblin.printStatus();
|
||||
|
||||
wizard.undoLastSpell();
|
||||
goblin.printStatus();
|
||||
|
||||
wizard.undoLastSpell();
|
||||
goblin.printStatus();
|
||||
|
||||
wizard.redoLastSpell();
|
||||
goblin.printStatus();
|
||||
|
||||
wizard.redoLastSpell();
|
||||
goblin.printStatus();
|
||||
}
|
||||
}
|
19
command/src/main/java/com/iluwatar/command/Command.java
Normal file
19
command/src/main/java/com/iluwatar/command/Command.java
Normal file
@@ -0,0 +1,19 @@
|
||||
package com.iluwatar.command;
|
||||
|
||||
/**
|
||||
*
|
||||
* Interface for Commands.
|
||||
*
|
||||
*/
|
||||
public abstract class Command {
|
||||
|
||||
public abstract void execute(Target target);
|
||||
|
||||
public abstract void undo();
|
||||
|
||||
public abstract void redo();
|
||||
|
||||
@Override
|
||||
public abstract String toString();
|
||||
|
||||
}
|
15
command/src/main/java/com/iluwatar/command/Goblin.java
Normal file
15
command/src/main/java/com/iluwatar/command/Goblin.java
Normal file
@@ -0,0 +1,15 @@
|
||||
package com.iluwatar.command;
|
||||
|
||||
public class Goblin extends Target {
|
||||
|
||||
public Goblin() {
|
||||
setSize(Size.NORMAL);
|
||||
setVisibility(Visibility.VISIBLE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Goblin";
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,31 @@
|
||||
package com.iluwatar.command;
|
||||
|
||||
public class InvisibilitySpell extends Command {
|
||||
|
||||
private 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 redo() {
|
||||
if (target != null) {
|
||||
target.setVisibility(Visibility.INVISIBLE);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Invisibility spell";
|
||||
}
|
||||
}
|
33
command/src/main/java/com/iluwatar/command/ShrinkSpell.java
Normal file
33
command/src/main/java/com/iluwatar/command/ShrinkSpell.java
Normal file
@@ -0,0 +1,33 @@
|
||||
package com.iluwatar.command;
|
||||
|
||||
public class ShrinkSpell extends Command {
|
||||
|
||||
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 undo() {
|
||||
if (oldSize != null && target != null) {
|
||||
Size temp = target.getSize();
|
||||
target.setSize(oldSize);
|
||||
oldSize = temp;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void redo() {
|
||||
undo();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Shrink spell";
|
||||
}
|
||||
}
|
22
command/src/main/java/com/iluwatar/command/Size.java
Normal file
22
command/src/main/java/com/iluwatar/command/Size.java
Normal file
@@ -0,0 +1,22 @@
|
||||
package com.iluwatar.command;
|
||||
|
||||
/**
|
||||
*
|
||||
* Enumeration for target size.
|
||||
*
|
||||
*/
|
||||
public enum Size {
|
||||
|
||||
SMALL("small"), NORMAL("normal"), LARGE("large"), UNDEFINED("");
|
||||
|
||||
private String title;
|
||||
|
||||
Size(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return title;
|
||||
}
|
||||
}
|
38
command/src/main/java/com/iluwatar/command/Target.java
Normal file
38
command/src/main/java/com/iluwatar/command/Target.java
Normal file
@@ -0,0 +1,38 @@
|
||||
package com.iluwatar.command;
|
||||
|
||||
/**
|
||||
*
|
||||
* Base class for spell targets.
|
||||
*
|
||||
*/
|
||||
public abstract class Target {
|
||||
|
||||
private Size size;
|
||||
|
||||
private Visibility visibility;
|
||||
|
||||
public Size getSize() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public void setSize(Size size) {
|
||||
this.size = size;
|
||||
}
|
||||
|
||||
public Visibility getVisibility() {
|
||||
return visibility;
|
||||
}
|
||||
|
||||
public void setVisibility(Visibility visibility) {
|
||||
this.visibility = visibility;
|
||||
}
|
||||
|
||||
@Override
|
||||
public abstract String toString();
|
||||
|
||||
public void printStatus() {
|
||||
System.out.println(String.format("%s, [size=%s] [visibility=%s]", this,
|
||||
getSize(), getVisibility()));
|
||||
System.out.println();
|
||||
}
|
||||
}
|
22
command/src/main/java/com/iluwatar/command/Visibility.java
Normal file
22
command/src/main/java/com/iluwatar/command/Visibility.java
Normal file
@@ -0,0 +1,22 @@
|
||||
package com.iluwatar.command;
|
||||
|
||||
/**
|
||||
*
|
||||
* Enumeration for target visibility.
|
||||
*
|
||||
*/
|
||||
public enum Visibility {
|
||||
|
||||
VISIBLE("visible"), INVISIBLE("invisible"), UNDEFINED("");
|
||||
|
||||
private String title;
|
||||
|
||||
Visibility(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return title;
|
||||
}
|
||||
}
|
45
command/src/main/java/com/iluwatar/command/Wizard.java
Normal file
45
command/src/main/java/com/iluwatar/command/Wizard.java
Normal file
@@ -0,0 +1,45 @@
|
||||
package com.iluwatar.command;
|
||||
|
||||
import java.util.Deque;
|
||||
import java.util.LinkedList;
|
||||
|
||||
public class Wizard extends Target {
|
||||
|
||||
private Deque<Command> undoStack = new LinkedList<>();
|
||||
private Deque<Command> redoStack = new LinkedList<>();
|
||||
|
||||
public Wizard() {
|
||||
setSize(Size.NORMAL);
|
||||
setVisibility(Visibility.VISIBLE);
|
||||
}
|
||||
|
||||
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 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";
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user