命令模式
java
//Invoker.java
package shiyan16;
public class Invoker {
private Command command;
public void setCommand(Command command) {
this.command =command;
}
/**
* 执行运算
* @param value
*/
public void addNum(int value) {
command.execute(value);
}
/**
* 撤回
*/
public void undo() {
int i = command.undo();
if(i==-1){
System.out.println("已撤销到初态");
}else{
System.out.println("执行成功,运算结果是:"+i);
}
}
/**
* 恢复
*/
public void redo() {
int i = command.redo();
if(i==-1){
System.out.println("已重复到终态");
}
else{
System.out.println("执行成功,运算结果是:"+i);
}
}
}
//Command.java
package shiyan16;
public abstract class Command {
public abstract int execute(int value);
public abstract int undo();
public abstract int redo();
}
//Adder.java
package shiyan16;
public class Adder {
private int num =0;
public int addOperate(int value) {
num+=value;
return num;
}
}
//AddCommand.java
package shiyan16;
import java.util.Stack;
public class AddCommand extends Command{
private Adder adder = new Adder();
private Stack<Integer> unStack = new Stack<Integer>();// 撤回栈,用来记录所做的每一步操作,用于撤回
private Stack<Integer> reStack = new Stack<Integer>();// 恢复栈,用来存储返回栈弹出的数据,用于恢复
/**
* 撤回
*/
public int undo() {
int i=0;
if (unStack.isEmpty()) {
i=-1;
}else{
Integer pop = unStack.pop();
reStack.push(pop); //将撤回栈中的栈顶元素弹出,并且压入恢复栈中
if(!unStack.isEmpty()){//判断弹出数据后是否为空,如果为空,说明已撤回到最原始状态
i=unStack.peek();
}
}
return i;
}
/**
* 恢复
*/
public int redo() {
int i=0;
if (reStack.isEmpty()) {
i=-1;
}else{//撤回时只要可以可以撤回,则撤回栈一定有数据
Integer pop = reStack.pop();
unStack.push(pop);
i=pop;
}
return i;
}
/**
* 执行计算,并进行栈的更新
*/
public int execute(int value) {
int v = 0;
v = adder.addOperate(value);
System.out.println(v);
unStack.push(v);
return v;
}
}
//Client.java
package shiyan16;
public class Client {
public static void main(String[] args) {
Invoker inv = new Invoker();
AddCommand command = new AddCommand();
inv.setCommand(command);
//计算
System.out.println("计算过程:");
inv.addNum(1);
inv.addNum(2);
inv.addNum(3);
//多次撤回
System.out.println("undo操作:");
inv.undo();
inv.undo();
inv.undo();
inv.undo();
//多次恢复
System.out.println("redo操作:");
inv.redo();
inv.redo();
inv.redo();
inv.redo();
}
}
c++
#include<iostream>
#include<vector>
#include<stack>
using namespace std;
//接受者
class Adder {
private:
int num;
public:
Adder(){
this->num=0;
}
int add(int value) {
num += value;
return num;
}
};
//抽象命令
class Command {
public:
virtual int execute(int value) = 0;
virtual int undo() = 0;
virtual int redo() = 0;
};
//具体命令
class AddCommand : public Command {
private:
Adder adder;
stack <int>unStack;
stack <int>reStack;
public:
int undo() {
int i = 0;
if (unStack.empty()) {
i = -1;
}
else {
reStack.push(unStack.top());
unStack.pop();
if (!unStack.empty()) {
i = unStack.top();
}
}
return i;
}
int redo() {
int i = 0;
if (reStack.empty()) {
i = -1;
}
else {//撤回时只要可以可以撤回,则撤回栈一定有数据
unStack.push(reStack.top());
i = reStack.top();
reStack.pop();
}
return i;
}
int execute(int value) {
int v = 0;
v = adder.add(value);
cout << v << endl;
unStack.push(v);
return v;
}
};
//调用者
class Invoker{
private:
AddCommand command;
public:
void setCommand(AddCommand command2) {
command = command2;
};
void compute(int value) {
command.execute(value);
};
void undo() {
int i = command.undo();
if (i == -1) {
cout << "已撤销到初态" << endl;
}
else {
cout << "执行成功,运算结果是:" << i << endl;
}
}
void redo() {
int i = command.redo();
if (i == -1) {
cout << "已恢复至终态" << endl;
}
else {
cout << "执行成功,运算结果是:" << i << endl;
}
}
};
//测试函数
int main() {
Invoker inv;
AddCommand command;
inv.setCommand(command);
//计算
cout << "计算过程:" << endl;
inv.compute(1);
inv.compute(2);
inv.compute(3);
//多次撤回
cout << "undo操作:" << endl;
inv.undo();
inv.undo();
inv.undo();
inv.undo();
//多次恢复
cout << "redo操作:" << endl;
inv.redo();
inv.redo();
inv.redo();
inv.redo();
}
//Invoker.java
package shiyan16;
public class Invoker {
private Command command;
public void setCommand(Command command) {
this.command =command;
}
/**
* 执行运算
* @param value
*/
public void addNum(int value) {
command.execute(value);
}
/**
* 撤回
*/
public void undo() {
int i = command.undo();
if(i==-1){
System.out.println("已撤销到初态");
}else{
System.out.println("执行成功,运算结果是:"+i);
}
}
/**
* 恢复
*/
public void redo() {
int i = command.redo();
if(i==-1){
System.out.println("已重复到终态");
}
else{
System.out.println("执行成功,运算结果是:"+i);
}
}
}
//Command.java
package shiyan16;
public abstract class Command {
public abstract int execute(int value);
public abstract int undo();
public abstract int redo();
}
//Adder.java
package shiyan16;
public class Adder {
private int num =0;
public int addOperate(int value) {
num+=value;
return num;
}
}
//AddCommand.java
package shiyan16;
import java.util.Stack;
public class AddCommand extends Command{
private Adder adder = new Adder();
private Stack<Integer> unStack = new Stack<Integer>();// 撤回栈,用来记录所做的每一步操作,用于撤回
private Stack<Integer> reStack = new Stack<Integer>();// 恢复栈,用来存储返回栈弹出的数据,用于恢复
/**
* 撤回
*/
public int undo() {
int i=0;
if (unStack.isEmpty()) {
i=-1;
}else{
Integer pop = unStack.pop();
reStack.push(pop); //将撤回栈中的栈顶元素弹出,并且压入恢复栈中
if(!unStack.isEmpty()){//判断弹出数据后是否为空,如果为空,说明已撤回到最原始状态
i=unStack.peek();
}
}
return i;
}
/**
* 恢复
*/
public int redo() {
int i=0;
if (reStack.isEmpty()) {
i=-1;
}else{//撤回时只要可以可以撤回,则撤回栈一定有数据
Integer pop = reStack.pop();
unStack.push(pop);
i=pop;
}
return i;
}
/**
* 执行计算,并进行栈的更新
*/
public int execute(int value) {
int v = 0;
v = adder.addOperate(value);
System.out.println(v);
unStack.push(v);
return v;
}
}
//Client.java
package shiyan16;
public class Client {
public static void main(String[] args) {
Invoker inv = new Invoker();
AddCommand command = new AddCommand();
inv.setCommand(command);
//计算
System.out.println("计算过程:");
inv.addNum(1);
inv.addNum(2);
inv.addNum(3);
//多次撤回
System.out.println("undo操作:");
inv.undo();
inv.undo();
inv.undo();
inv.undo();
//多次恢复
System.out.println("redo操作:");
inv.redo();
inv.redo();
inv.redo();
inv.redo();
}
}
c++
#include<iostream>
#include<vector>
#include<stack>
using namespace std;
//接受者
class Adder {
private:
int num;
public:
Adder(){
this->num=0;
}
int add(int value) {
num += value;
return num;
}
};
//抽象命令
class Command {
public:
virtual int execute(int value) = 0;
virtual int undo() = 0;
virtual int redo() = 0;
};
//具体命令
class AddCommand : public Command {
private:
Adder adder;
stack <int>unStack;
stack <int>reStack;
public:
int undo() {
int i = 0;
if (unStack.empty()) {
i = -1;
}
else {
reStack.push(unStack.top());
unStack.pop();
if (!unStack.empty()) {
i = unStack.top();
}
}
return i;
}
int redo() {
int i = 0;
if (reStack.empty()) {
i = -1;
}
else {//撤回时只要可以可以撤回,则撤回栈一定有数据
unStack.push(reStack.top());
i = reStack.top();
reStack.pop();
}
return i;
}
int execute(int value) {
int v = 0;
v = adder.add(value);
cout << v << endl;
unStack.push(v);
return v;
}
};
//调用者
class Invoker{
private:
AddCommand command;
public:
void setCommand(AddCommand command2) {
command = command2;
};
void compute(int value) {
command.execute(value);
};
void undo() {
int i = command.undo();
if (i == -1) {
cout << "已撤销到初态" << endl;
}
else {
cout << "执行成功,运算结果是:" << i << endl;
}
}
void redo() {
int i = command.redo();
if (i == -1) {
cout << "已恢复至终态" << endl;
}
else {
cout << "执行成功,运算结果是:" << i << endl;
}
}
};
//测试函数
int main() {
Invoker inv;
AddCommand command;
inv.setCommand(command);
//计算
cout << "计算过程:" << endl;
inv.compute(1);
inv.compute(2);
inv.compute(3);
//多次撤回
cout << "undo操作:" << endl;
inv.undo();
inv.undo();
inv.undo();
inv.undo();
//多次恢复
cout << "redo操作:" << endl;
inv.redo();
inv.redo();
inv.redo();
inv.redo();
}

浙公网安备 33010602011771号