class Command{
public:
virtual bool execute() = 0;
virtual bool Unexecute() = 0;
};
class operator1 : public Command{
public:
bool execute(){
std::cout << "Operator1" << std::endl;
return true;
}
bool Unexecute(){
std::cout << "Unexecute Operator2" << std::endl;
return true;
}
};
class operator2 : public Command{
public:
bool execute(){
std::cout << "Operator2" << std::endl;
return true;
}
bool Unexecute(){
std::cout << "Unexecute Operator2" << std::endl;
return true;
}
};
class CommandManager
{
public:
static CommandManager* instance(){
static CommandManager ins;
return &ins;
}
void callCommand(Command* pCommand){
if (pCommand != NULL)
{
if (pCommand->execute())
{
PushUndoCommand(pCommand);
clearRedoCommand();
}
else
{
delete pCommand;
}
}
}
void PushUndoCommand(Command* pCommand){
C++实现Undo和Redo框架(命令模式)
最新推荐文章于 2022-05-12 11:15:47 发布
本文介绍了如何使用C++实现一个基于命令模式的撤销(Undo)和重做(Redo)框架。通过定义Command基类,以及operator1和operator2两个子类来执行和撤销操作。CommandManager类作为管理器,维护了两个堆栈,分别用于存储可撤销和可重做的命令,提供了undo和redo方法来执行相应的操作。
摘要由CSDN通过智能技术生成