Merge pull request from zhwbqd/master

add redo function into command pattern
This commit is contained in:
Ilkka Seppälä
2015-04-17 07:56:28 +03:00
8 changed files with 51 additions and 5 deletions
.gitignore
adapter/src/main/java/com/iluwatar
command/src/main/java/com/iluwatar

4
.gitignore vendored

@@ -8,3 +8,7 @@ target
*.jar
*.war
*.ear
.idea
*.iml
*.swp

@@ -15,7 +15,7 @@ package com.iluwatar;
public class App {
public static void main(String[] args) {
GnomeEngineeringManager manager = new GnomeEngineeringManager();
Engineer manager = new GnomeEngineeringManager();
manager.operateDevice();
}
}

@@ -20,7 +20,17 @@ public class App {
wizard.castSpell(new InvisibilitySpell(), goblin);
goblin.printStatus();
wizard.undoLastSpell();
goblin.printStatus();
wizard.undoLastSpell();
goblin.printStatus();
wizard.redoLastSpell();
goblin.printStatus();
wizard.redoLastSpell();
goblin.printStatus();
}
}

@@ -11,6 +11,8 @@ public abstract class Command {
public abstract void undo();
public abstract void redo();
@Override
public abstract String toString();

@@ -17,6 +17,13 @@ public class InvisibilitySpell extends Command {
}
}
@Override
public void redo() {
if (target != null) {
target.setVisibility(Visibility.INVISIBLE);
}
}
@Override
public String toString() {
return "Invisibility spell";

@@ -15,10 +15,17 @@ public class ShrinkSpell extends Command {
@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";

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

@@ -1,8 +1,12 @@
package com.iluwatar;
import java.util.Deque;
import java.util.LinkedList;
public class Wizard extends Target {
private Command previousSpell;
private Deque<Command> undoStack = new LinkedList<>();
private Deque<Command> redoStack = new LinkedList<>();
public Wizard() {
setSize(Size.NORMAL);
@@ -12,16 +16,27 @@ public class Wizard extends Target {
public void castSpell(Command command, Target target) {
System.out.println(this + " casts " + command + " at " + target);
command.execute(target);
previousSpell = command;
undoStack.offerLast(command);
}
public void undoLastSpell() {
if (previousSpell != null) {
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";