Skip to content

Commit d66e63a

Browse files
committed
Merge pull request iluwatar#46 from zhwbqd/master
add redo function into command pattern
2 parents 935cdd0 + c5da37a commit d66e63a

File tree

8 files changed

+51
-5
lines changed

8 files changed

+51
-5
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,7 @@ target
88
*.jar
99
*.war
1010
*.ear
11+
.idea
12+
*.iml
13+
*.swp
14+

adapter/src/main/java/com/iluwatar/App.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
public class App {
1616

1717
public static void main(String[] args) {
18-
GnomeEngineeringManager manager = new GnomeEngineeringManager();
18+
Engineer manager = new GnomeEngineeringManager();
1919
manager.operateDevice();
2020
}
2121
}

command/src/main/java/com/iluwatar/App.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,17 @@ public static void main(String[] args) {
2020

2121
wizard.castSpell(new InvisibilitySpell(), goblin);
2222
goblin.printStatus();
23+
24+
wizard.undoLastSpell();
25+
goblin.printStatus();
26+
2327
wizard.undoLastSpell();
2428
goblin.printStatus();
29+
30+
wizard.redoLastSpell();
31+
goblin.printStatus();
32+
33+
wizard.redoLastSpell();
34+
goblin.printStatus();
2535
}
2636
}

command/src/main/java/com/iluwatar/Command.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ public abstract class Command {
1111

1212
public abstract void undo();
1313

14+
public abstract void redo();
15+
1416
@Override
1517
public abstract String toString();
1618

command/src/main/java/com/iluwatar/InvisibilitySpell.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ public void undo() {
1717
}
1818
}
1919

20+
@Override
21+
public void redo() {
22+
if (target != null) {
23+
target.setVisibility(Visibility.INVISIBLE);
24+
}
25+
}
26+
2027
@Override
2128
public String toString() {
2229
return "Invisibility spell";

command/src/main/java/com/iluwatar/ShrinkSpell.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,17 @@ public void execute(Target target) {
1515
@Override
1616
public void undo() {
1717
if (oldSize != null && target != null) {
18+
Size temp = target.getSize();
1819
target.setSize(oldSize);
20+
oldSize = temp;
1921
}
2022
}
2123

24+
@Override
25+
public void redo() {
26+
undo();
27+
}
28+
2229
@Override
2330
public String toString() {
2431
return "Shrink spell";

command/src/main/java/com/iluwatar/Target.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ public void setVisibility(Visibility visibility) {
3131
public abstract String toString();
3232

3333
public void printStatus() {
34-
System.out.println(String.format("%s, size=%s visibility=%s", this,
34+
System.out.println(String.format("%s, [size=%s] [visibility=%s]", this,
3535
getSize(), getVisibility()));
36+
System.out.println();
3637
}
3738
}

command/src/main/java/com/iluwatar/Wizard.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package com.iluwatar;
22

3+
import java.util.Deque;
4+
import java.util.LinkedList;
5+
36
public class Wizard extends Target {
47

5-
private Command previousSpell;
8+
private Deque<Command> undoStack = new LinkedList<>();
9+
private Deque<Command> redoStack = new LinkedList<>();
610

711
public Wizard() {
812
setSize(Size.NORMAL);
@@ -12,16 +16,27 @@ public Wizard() {
1216
public void castSpell(Command command, Target target) {
1317
System.out.println(this + " casts " + command + " at " + target);
1418
command.execute(target);
15-
previousSpell = command;
19+
undoStack.offerLast(command);
1620
}
1721

1822
public void undoLastSpell() {
19-
if (previousSpell != null) {
23+
if (!undoStack.isEmpty()) {
24+
Command previousSpell = undoStack.pollLast();
25+
redoStack.offerLast(previousSpell);
2026
System.out.println(this + " undoes " + previousSpell);
2127
previousSpell.undo();
2228
}
2329
}
2430

31+
public void redoLastSpell() {
32+
if (!redoStack.isEmpty()) {
33+
Command previousSpell = redoStack.pollLast();
34+
undoStack.offerLast(previousSpell);
35+
System.out.println(this + " redoes " + previousSpell);
36+
previousSpell.redo();
37+
}
38+
}
39+
2540
@Override
2641
public String toString() {
2742
return "Wizard";

0 commit comments

Comments
 (0)