#include
#include
using namespace std;
//rzhetsky abstract class
The class rzhetsky
{
//empty
};
//define command interface
The class Command
{
Public:
//execute the command
Virtual void the execute ()=0;
//cancel the command to restore the state of the object to the memo record
Virtual void undo (rzhetsky * m)=0.
//redo command and restore the state of the object to the memo record
Virtual void redo (rzhetsky * m)=0.
//create memo object
Virtual rzhetsky * createMemento ()=0;
};
//operation operation interface
The class OperationApi
{
Public:
Virtual int getResult ()=0;
Virtual void the add (int num)=0;
Virtual void substract (int num)=0;
//save the primary device object state create the memo object
Virtual rzhetsky * createMemento ()=0;
//reset the primary object state, let it back to the memo object
//record of the state,
Virtual void setMemento (rzhetsky * m)=0.
};
//command object of public objects, common methods for the achievement of the command object
The class AbstractCommand: public Command
{
Protected:
//hold true command object implementer
OperationApi * operation;
Public:
Virtual void the execute ()=0;
Void setOperation (OperationApi * operation)
{
This - & gt; Operation=operation;
}
Void undo (rzhetsky * m)
{
Operation - & gt; SetMemento (m);
}
Void redo (rzhetsky * m)
{
Operation - & gt; SetMemento (m);
}
Rzhetsky * createMemento ()
{
The return operation - & gt; CreateMemento ();
}
};
//add command
The class AddCommand: public AbstractCommand
{
Private:
Int opeNum;
Public:
AddCommand (int opeNum)
{
This - & gt; OpeNum=opeNum;
}
Void the execute ()
{
Operation - & gt; Add (opeNum);
}
};
//subtraction command
The class SubstractCommand: public AbstractCommand
{
Private:
Int opeNum;
Public:
SubstractCommand (int opeNum)
{
This - & gt; OpeNum=opeNum;
}
Void the execute ()
{
Operation - & gt; Substract (opeNum);
}
};
//operation (equivalent to the primary objects)
//in comparison with the original, no longer provide setResult method, internal state, does not allow external to operate
//add createMemento and setMemento method the implementation of the
//add implements a private memo object
The class Operation: public OperationApi
{
Private:
int result;//the results
//inner class
The class MementoImpl: public rzhetsky
{
Private:
int result;
Public:
MementoImpl (int result)
{
This - & gt; Result=the result;
}
Int getResult () {return result; }
};
Public:
Int getResult () {return result; }
Void the add (int num) {result +=num; }
Void substract (int num) {result -=num; }
Rzhetsky * createMemento ()
{
MementoImpl * m=new MementoImpl (result);
Return m;
}
Void setMemento (rzhetsky * m)
{
MementoImpl * impl=(MementoImpl *) m;
Result=impl - & gt; GetResult ();
}
};
//the calculator class (a add button, subtract button, and undo and restore button)
The class Calculator
{
Private:
//command operation history, when the cancellation with
List
List
//every command is executed before and after the two state
//and the matching, that is, each executing a command, will first before the execution of the press in
//state, and then pressed into execution state
List
The Command * subCmd;
Public:
Void setAddCmd (Command * addCmd)
{
This - & gt; AddCmd=addCmd;
}
Void setSubstractCmd (Command * subCmd)
{
This - & gt; SubCmd=subCmd;
}
//XiaJiaFa button
Void addPressed ()
{
//backup before executing commands,
Rzhetsky * m1=addCmd - & gt; CreateMemento ();
//execute the command
AddCmd - & gt; The execute ();
//operation records to the history
UndoCmds. Push_back (addCmd);
//execute the command again after backup
Rzhetsky * m2=addCmd - & gt; CreateMemento ();
//set to undo history (before the pressure into the command execution, respectively, after the two state)
UndoMementos. Push_back (m1);
UndoMementos. Push_back (m2);
}
//subtraction buttons
Void substractPressed ()
{
//backup before executing commands,
Rzhetsky * m1=subCmd - & gt; CreateMemento ();
//execute the command
SubCmd - & gt; The execute ();
//operation records to the history
UndoCmds. Push_back (subCmd);
//execute the command again after backup
Rzhetsky * m2=subCmd - & gt; CreateMemento ();
//set to undo history (before the pressure into the command execution, respectively, after the two state)
UndoMementos. Push_back (m1);
UndoMementos. Push_back (m2);
}
//undo
Void undoPressed ()
{
If (undoCmds. The size () & gt; 0)
{
//remove the last command to undo
The Command * CMD=undoCmds. Back ();
//get the corresponding memo object
Rzhetsky * m2=undoMementos. Back ();
UndoMementos. Pop_back ();
Rzhetsky * m1=undoMementos. Back ();
UndoMementos. Pop_back ();
//cancel (of course is to go back to the button before the execution of the state)
CMD - & gt; To undo (m1);
//add restore function
RedoCmds. Push_back (CMD);
//add the corresponding case record object also past
RedoMementos. Push_back (m1);
RedoMementos. Push_back (m2);
//deleted from the undo the last has executed command)
UndoCmds. Pop_back ();
}
The else
{
cout <" I'm sorry, there is no undo command!"
}
//restore operations
Void redoPressed ()
{
If (redoCmds. The size () & gt; 0)
{
//remove the last command to redo
The Command * CMD=redoCmds. Back ();
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull