命令模式javac++

软件设计                  石家庄铁道大学信息学院

 

实验16:命令模式

本次实验属于模仿型实验,通过本次实验学生将掌握以下内容: 

1、理解命令模式的动机,掌握该模式的结构;

2、能够利用命令模式解决实际问题。

 

[实验任务一]:多次撤销和重复的命令模式

某系统需要提供一个命令集合(注:可以使用链表,栈等集合对象实现),用于存储一系列命令对象,并通过该命令集合实现多次undo()redo()操作,可以使用加法运算来模拟实现。

实验要求:

1. 提交类图;

 

 

2. 提交源代码;

 

 

 

 

3. 注意编程规范。

 

 

 

package test16;

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;
    }
}
package test16;

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);
        }
    }
}
package test16;

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(9);
        inv.addNum(2);
        inv.addNum(5);
        //多次撤回
        System.out.println("undo:");
        inv.undo();
        inv.undo();
        inv.undo();
        inv.undo();
        //多次恢复
        System.out.println("redo:");
        inv.redo();
        inv.redo();
        inv.redo();
        inv.redo();
    }
}
package test16;

public abstract class Command {
    public abstract int execute(int value);
    public abstract int undo();
    public abstract int redo();
}
package test16;

public class Adder {
    private int num =0;
    public int addOperate(int value) {
        num+=value;
        return num;
    }
}
#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(9);
    inv.compute(2);
    inv.compute(5);
    //多次撤回
    cout << "undo:" << endl;
    inv.undo();
    inv.undo();
    inv.undo();
    inv.undo();
    //多次恢复
    cout << "redo:" << endl;
    inv.redo();
    inv.redo();
    inv.redo();
    inv.redo();
}

 

 

posted @ 2022-12-23 17:00  zrswheart  阅读(34)  评论(0编辑  收藏  举报