1. 程式人生 > 其它 >軟體設計-命令模式

軟體設計-命令模式

多次撤銷和重複的命令模式
某系統需要提供一個命令集合(注:可以使用連結串列,棧等集合物件實現),用於儲存一系列命令物件,並通過該命令集合實現多次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;
}

執行截圖