Skip to content

Commit f4bdf79

Browse files
committed
undo command
1 parent db1d7aa commit f4bdf79

File tree

8 files changed

+354
-258
lines changed

8 files changed

+354
-258
lines changed

.idea/workspace.xml

Lines changed: 227 additions & 258 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.tao.patterns.ch6command.undo;
2+
3+
public class AddCommand extends Command {
4+
5+
private Adder adder = new Adder();
6+
7+
8+
@Override
9+
int execute(int value) {
10+
result = adder.add(result, value);
11+
super.num = value;
12+
System.out.println("+" + value + "=" + result);
13+
return result;
14+
}
15+
16+
@Override
17+
int undo() {
18+
result = adder.add(result, -num);
19+
return result;
20+
}
21+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.tao.patterns.ch6command.undo;
2+
3+
public class Adder {
4+
5+
public int add(int num1, int num2) {
6+
return num1 + num2;
7+
}
8+
}
9+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.tao.patterns.ch6command.undo;
2+
3+
public class CalculatorForm {
4+
5+
private Command command;
6+
7+
public CalculatorForm(Command command) {
8+
this.command = command;
9+
}
10+
11+
public void setCommand(Command command) {
12+
this.command = command;
13+
}
14+
15+
public void computer(int num) {
16+
int result = command.execute(num);
17+
System.out.println("计算结果为:" + result);
18+
}
19+
20+
public void undo() {
21+
int result = command.undo();
22+
System.out.println("后退的结果为:" + result);
23+
}
24+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.tao.patterns.ch6command.undo;
2+
3+
public abstract class Command {
4+
protected static int result = 0;
5+
protected int num = 0;
6+
7+
abstract int execute(int num);
8+
abstract int undo();
9+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.tao.patterns.ch6command.undo;
2+
3+
/**
4+
* 命令模式的undo
5+
*
6+
* 一个加减法计算器
7+
* 可连续进行加减,但是undo只能进行一次,否则数据会出现错误
8+
*/
9+
public class Main {
10+
11+
public static void main(String[] args) {
12+
Main main = new Main();
13+
main.fun1();
14+
15+
}
16+
17+
private void fun1() {
18+
Command command = new AddCommand();
19+
20+
21+
CalculatorForm calculatorForm = new CalculatorForm(command);
22+
calculatorForm.computer(10);
23+
calculatorForm.computer(5);
24+
calculatorForm.undo();
25+
26+
command = new SubCommand();
27+
calculatorForm.setCommand(command);
28+
calculatorForm.computer(3);
29+
calculatorForm.computer(4);
30+
calculatorForm.undo();
31+
//undo只能后退一步
32+
// calculatorForm.undo();
33+
34+
35+
}
36+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.tao.patterns.ch6command.undo;
2+
3+
public class SubCommand extends Command{
4+
5+
private Suber suber = new Suber();
6+
7+
@Override
8+
int execute(int value) {
9+
super.num = value;
10+
result = suber.subtraction(result, value);
11+
System.out.println("-" + value + "=" + result);
12+
return result;
13+
}
14+
15+
@Override
16+
int undo() {
17+
result = suber.subtraction(result, -num);
18+
return result;
19+
}
20+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.tao.patterns.ch6command.undo;
2+
3+
public class Suber {
4+
5+
public int subtraction(int num1, int num2){
6+
return num1 - num2;
7+
}
8+
}

0 commit comments

Comments
 (0)