add redo function on command pattern
This commit is contained in:
parent
33566805ee
commit
120555f740
@ -20,7 +20,17 @@ public class App {
|
|||||||
|
|
||||||
wizard.castSpell(new InvisibilitySpell(), goblin);
|
wizard.castSpell(new InvisibilitySpell(), goblin);
|
||||||
goblin.printStatus();
|
goblin.printStatus();
|
||||||
|
|
||||||
wizard.undoLastSpell();
|
wizard.undoLastSpell();
|
||||||
goblin.printStatus();
|
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 undo();
|
||||||
|
|
||||||
|
public abstract void redo();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public abstract String toString();
|
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
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "Invisibility spell";
|
return "Invisibility spell";
|
||||||
|
@ -15,10 +15,17 @@ public class ShrinkSpell extends Command {
|
|||||||
@Override
|
@Override
|
||||||
public void undo() {
|
public void undo() {
|
||||||
if (oldSize != null && target != null) {
|
if (oldSize != null && target != null) {
|
||||||
|
Size temp = target.getSize();
|
||||||
target.setSize(oldSize);
|
target.setSize(oldSize);
|
||||||
|
oldSize = temp;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void redo() {
|
||||||
|
undo();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "Shrink spell";
|
return "Shrink spell";
|
||||||
|
@ -31,7 +31,8 @@ public abstract class Target {
|
|||||||
public abstract String toString();
|
public abstract String toString();
|
||||||
|
|
||||||
public void printStatus() {
|
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()));
|
getSize(), getVisibility()));
|
||||||
|
System.out.println();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,12 @@
|
|||||||
package com.iluwatar;
|
package com.iluwatar;
|
||||||
|
|
||||||
|
import java.util.Deque;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
|
||||||
public class Wizard extends Target {
|
public class Wizard extends Target {
|
||||||
|
|
||||||
private Command previousSpell;
|
private Deque<Command> undoStack = new LinkedList<>();
|
||||||
|
private Deque<Command> redoStack = new LinkedList<>();
|
||||||
|
|
||||||
public Wizard() {
|
public Wizard() {
|
||||||
setSize(Size.NORMAL);
|
setSize(Size.NORMAL);
|
||||||
@ -12,16 +16,27 @@ public class Wizard extends Target {
|
|||||||
public void castSpell(Command command, Target target) {
|
public void castSpell(Command command, Target target) {
|
||||||
System.out.println(this + " casts " + command + " at " + target);
|
System.out.println(this + " casts " + command + " at " + target);
|
||||||
command.execute(target);
|
command.execute(target);
|
||||||
previousSpell = command;
|
undoStack.offerLast(command);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void undoLastSpell() {
|
public void undoLastSpell() {
|
||||||
if (previousSpell != null) {
|
if (!undoStack.isEmpty()) {
|
||||||
|
Command previousSpell = undoStack.pollLast();
|
||||||
|
redoStack.offerLast(previousSpell);
|
||||||
System.out.println(this + " undoes " + previousSpell);
|
System.out.println(this + " undoes " + previousSpell);
|
||||||
previousSpell.undo();
|
previousSpell.undo();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void redoLastSpell() {
|
||||||
|
if (!redoStack.isEmpty()) {
|
||||||
|
Command previousSpell = redoStack.pollLast();
|
||||||
|
undoStack.offerLast(previousSpell);
|
||||||
|
System.out.println(this + " redoes " + previousSpell);
|
||||||
|
previousSpell.redo();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "Wizard";
|
return "Wizard";
|
||||||
|
Loading…
x
Reference in New Issue
Block a user