S
ich finde die folgende Methode sehr nutzbar und hab sie schon mehr mals in unterschidlichen Projecten verwendet.
class ICommand
{
public:
virtual void Do() = 0;
virtual void Undo() = 0;
virtual void Redo() = 0;
};
class ICommandProcessor
{
public:
virtual execute(ICommand *cmd) = 0;
virtual process(ICommand *cmd) = 0;
};
class InsertText : public ICommand
{
public:
InsertText(ITextStory *story, TextIndex position, std::wstring &text);
};
class DeleteText : public ICommand
{
DeleteText(ITextStory *story, const TextRange &range);
};
ICommand *insertTextCmd(ITextStory *story, TextIndex position, std::wstring &text)
{
return new InsertText(story, position, text);
}
ICommand *deleteTextCmd(ITextStory *story, const TextRange &range)
{
return new DeleteText(story, range);
}
int main()
{
smatr_ptr<ICommandProcessor> cmdProc(...);
smatr_ptr<ICommandSequence> cmdSeq(...);
try
{
cmdProc->process(insertTextCmd(
textModel, 11, "aaaaaaaaabbbbbbccccc")));
cmdProc->process(deleteTextCmd(
textModel, TextRange(0, 23)));
cmdSec->Commit();
}
catch(std::exception &e)
{
cmdSec->Abort();
//...
}
return 0;
}