软件设计16|命令模式
(1)Java版本
效果:
工程目录:
代码:
AbstractCommand.java
1 package command_mode; 2 3 public abstract class AbstractCommand { 4 public abstract int execute(int value); 5 public abstract int undo(); 6 public abstract int redo(); 7 }
Adder.java
1 package command_mode; 2 3 public class Adder { 4 private int number=0; 5 public int add(int value) { 6 number+=value; 7 return number; 8 } 9 }
CalculatorForm.java
1 package command_mode; 2 3 public class CalculatorForm { 4 public AbstractCommand command; 5 public void setAbstractCommand(AbstractCommand command) { 6 this.command=command; 7 } 8 public void computer(int value) { 9 int i=command.execute(value); 10 System.out.println("执行运算,运算结果为:"+i); 11 } 12 public void undo() { 13 int i=command.undo(); 14 System.out.println("执行undo,运算结果为:"+i); 15 } 16 public void redo() { 17 int i=command.redo(); 18 System.out.println("执行redo,运算结果为:"+i); 19 } 20 }
Client.java
1 package command_mode; 2 3 public class Client { 4 public static void main(String[] args) { 5 CalculatorForm form=new CalculatorForm(); 6 AbstractCommand command=new ConcreteCommand(); 7 form.setAbstractCommand(command); 8 form.computer(10); 9 form.computer(20); 10 form.undo(); 11 form.redo(); 12 form.computer(30); 13 form.undo(); 14 form.redo(); 15 } 16 }
ConcreteCommand.java
1 package command_mode; 2 3 public class ConcreteCommand extends AbstractCommand { 4 5 public Adder adder=new Adder(); 6 public int value; 7 8 @Override 9 public int execute(int value) { 10 // TODO Auto-generated method stub 11 this.value=value; 12 return adder.add(value); 13 } 14 15 @Override 16 public int undo() { 17 // TODO Auto-generated method stub 18 return adder.add(-value); 19 } 20 21 @Override 22 public int redo() { 23 // TODO Auto-generated method stub 24 return adder.add(value); 25 } 26 }
(2)C++版本
效果如下:
代码如下:
1 #include<iostream> 2 using namespace std; 3 4 class AbstractCommand{ 5 public: 6 virtual int execute(int value) = 0; 7 virtual int undo() = 0; 8 virtual int redo() = 0; 9 }; 10 11 class Adder { 12 private: 13 int number = 0; 14 public: 15 int add(int value) { 16 number = number + value; 17 return number; 18 } 19 }; 20 21 class ConcreteCommand :public AbstractCommand{ 22 private: 23 Adder* adder = new Adder(); 24 int _value=0; 25 public: 26 int execute(int value) { 27 _value = value; 28 return adder->add(_value); 29 } 30 int undo() { 31 return adder->add(-_value); 32 } 33 int redo() { 34 return adder->add(_value); 35 } 36 }; 37 38 class CalculatorForm{ 39 private: 40 AbstractCommand* _command; 41 public: 42 void setAbstractCommand(AbstractCommand* command) { 43 _command = command; 44 } 45 void computer(int value) { 46 int i = _command->execute(value); 47 cout << "执行运算,运算结果为:" << i << endl; 48 } 49 void undo() { 50 int i = _command->undo(); 51 cout << "执行undo,运算结果为:" << i << endl; 52 } 53 void redo() { 54 int i = _command->redo(); 55 cout << "执行redo,运算结果为:" << i << endl; 56 } 57 }; 58 59 int main() { 60 CalculatorForm* form = new CalculatorForm(); 61 AbstractCommand* command = new ConcreteCommand(); 62 form->setAbstractCommand(command); 63 form->computer(10); 64 form->computer(20); 65 form->undo(); 66 form->redo(); 67 form->computer(30); 68 form->undo(); 69 form->redo(); 70 }