zoukankan      html  css  js  c++  java
  • 软件设计命令模式

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

    类图

    java

    package rjsj.no16;
    
    abstract class AbstractCommand{//抽象命令
        public abstract int execute(int value);
        public abstract int undo();
        public abstract int redo();
    }
    
    class ConcreteCommand extends AbstractCommand{//具体命令
        private Adder adder = new Adder();
        private int value;
    
        public int execute(int value) {
            this.value=value;
            return adder.add(value);
        }
    
        public int undo() {
            return adder.add(-value);
        }
    
        public int redo() {
            return adder.add(+value);
        }
    }
    
    class CalculatorForm {//调用者
        private AbstractCommand command;
    
        public void setCommand(AbstractCommand command) {
            this.command=command;
        }
    
        //业务方法,用于调用命令类的方法
        public void compute(int value) {
            int i = command.execute(value);
            System.out.println("执行运算,运算结果为:" + i);
        }
    
        public void undo() {
            int i = command.undo();
            System.out.println("执行撤销,运算结果为:" + i);
        }
    
        public void redo() {
            int i = command.redo();
            System.out.println("执行重复,运算结果为:" + i);
        }
    }
    
    class Adder {//接收者
        private int num=0;
    
        public int add(int value) {
            num+=value;
            return num;
        }
    }
    
    class Client {//客户类
        public static void main(String args[]) {
            CalculatorForm form = new CalculatorForm();
            ConcreteCommand command = new ConcreteCommand();
            form.setCommand(command);
    
            form.compute(10);
            form.redo();
            form.compute(5);
            form.compute(10);
            form.undo();
    
        }
    }

    C++

    #include<iostream>
    #include<stack>
    using namespace std;
    int num=0;
    class AbstractCommand {
    public:
        virtual int execute(int value)=0;
        virtual int undo()=0;
        virtual int redo()=0;
    };
    class Adder {
    
    public:
        int add(int value) {
            num+=value;
            return num;
        }
    };
    class AddCommand :public AbstractCommand {
    private:
        Adder *adder;
        stack<int> unStack;
        stack<int> reStack;
    public:
        int undo() {
            int i=0;
            if (unStack.empty()) {
                i=-1;
            }else{
                int pop = unStack.top();
                reStack.push(pop);
                unStack.pop();
                if(!unStack.empty()){
                    i=unStack.top();
                }
            }
    
            return i;
        }
        int redo() {
             int i=0;
             if (reStack.empty()) {
                 i=-1;
             }else{
                 int pop = reStack.top();
                 reStack.pop();
                 unStack.push(pop);
                 i=pop;
    
             }
             return i;
         }
         int execute(int value) {
    
             int v = 0;
             if (unStack.empty()) {
                 v = adder->add(value);
                 unStack.push(v);
             } else {
                 v = adder->add(value);
                 unStack.push(v);
                 if (!reStack.empty()) {
                     for (int i = 0; i < reStack.size(); i++) {
                     }
                 }
             }
             return v;
         }
    };
    class CalculatorForm {
    private:
        AbstractCommand *command;
    public:
        void setCommand(AbstractCommand *command) {
            this->command =command;
        }
        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(){
        CalculatorForm *form = new CalculatorForm();
        AddCommand *command = new AddCommand();
        form->setCommand(command);
        //计算
        cout<<"------计算过程------"<<endl;
        form->compute(1);
        form->compute(2);
        form->compute(3);
        form->compute(4);
        //多次撤回
        cout<<"------撤回过程------"<<endl;
        form->undo();
        form->undo();
        form->undo();
        form->undo();
        form->undo();
        //多次恢复
        cout<<"------恢复过程------"<<endl;
        form->redo();
        form->redo();
        form->redo();
        form->redo();
        form->redo();
        return 0;
    }

    运行截图

  • 相关阅读:
    BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊
    算法笔记-- 二进制集合枚举子集 && 求子集和 && 求父集和
    BZOJ 1084: [SCOI2005]最大子矩阵
    BZOJ 1968: [Ahoi2005]COMMON 约数研究
    2018 German Collegiate Programming Contest (GCPC 18)
    Codeforces 1100 F
    算法笔记--极大极小搜索及alpha-beta剪枝
    2017 Russian Code Cup (RCC 17), Elimination Round D
    All You Can Code 2008 (Romanian Contest) A
    2016 Russian Code Cup (RCC 16), Final Round B
  • 原文地址:https://www.cnblogs.com/Arisf/p/15685679.html
Copyright © 2011-2022 走看看