File tree Expand file tree Collapse file tree 8 files changed +354
-258
lines changed
ch6-command/src/main/java/com/tao/patterns/ch6command/undo Expand file tree Collapse file tree 8 files changed +354
-258
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments