惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

I
InfoQ
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Apple Machine Learning Research
Apple Machine Learning Research
月光博客
月光博客
B
Blog
罗磊的独立博客
GbyAI
GbyAI
博客园 - 三生石上(FineUI控件)
雷峰网
雷峰网
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Microsoft Security Blog
Microsoft Security Blog
宝玉的分享
宝玉的分享
The GitHub Blog
The GitHub Blog
人人都是产品经理
人人都是产品经理
博客园 - Franky
有赞技术团队
有赞技术团队
WordPress大学
WordPress大学
博客园 - 聂微东
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
V
Visual Studio Blog
MyScale Blog
MyScale Blog
Google DeepMind News
Google DeepMind News
G
Google Developers Blog
aimingoo的专栏
aimingoo的专栏

博客园 - StationaryTraveller

如何查找微软VS软件中的图标 SQLite 的自增(AUTOINCREMENT)强制要求列的数据类型必须是INTEGER Qt动态获取翻译文本 Windows判断某窗口是否被其他窗口完全覆盖 MFC实现一个通用模态进度条窗口 如何批量修改“后期生成事件”为“预生成事件” MFC对话框中如何给一个控件发送消息 Window剪切板 C语言根据函数指针偏移实现函数动态调用 Qt程序系统文本翻译 SVN创建分支 Qt多版本如何共存 Qt6编译的程序在某些win10系统报错 电脑忘机了用户名密码怎么办 Qt事件过滤器实现空闲检测 CMFCToolTipCtrl的AddTool导致内存增加 MFC中CBitmap、CBrush、CFont、CPalette、CPen、CRgn删除GDI对象问题 实现从QListWidgett拖拽项到QTableWidget Windows系统下通过命令行获取进程指标 避免溢出求平均值的算法 字节流转16进制字符串 PImpl:Pointer to Implementation C++单例
命令模式实现撤销和重做机制
StationaryTraveller · 2024-11-15 · via 博客园 - StationaryTraveller
#include <vector>
#include <iostream>

//定义命令接口
class Command 
{
public:
	virtual void execute() = 0;
	virtual void undo() = 0;
};

//实现“增加”命令类
class IncreaseCommand : public Command 
{
private:
	int* _value;
	int _amount;
public:
	IncreaseCommand(int* value, int amount) : _value(value), _amount(amount) 
	{

	}

	void execute() override 
	{
		*_value += _amount; 
	}
	
	void undo() override 
	{ 
		*_value -= _amount; 
	}
};

//实现“减少”命令类
class DecreaseCommand : public Command 
{
private:
	int* _value;
	int _amount;
public:
	DecreaseCommand(int* value, int amount) : _value(value), _amount(amount) 
	{

	}

	void execute() override 
	{
		*_value -= _amount; 
	}

	void undo() override 
	{
		*_value += _amount; 
	}
};

//实现命令管理器
class CommandManager 
{
private:
	std::vector<Command*> _executedCommands;
	std::vector<Command*> _undoneCommands;
public:
	CommandManager() {}
	~CommandManager()
	{
		for (auto& cmd : _executedCommands)
		{
			delete cmd;
		}

		for (auto& cmd : _undoneCommands)
		{
			delete cmd;
		}
	}

	void execute(Command* command) 
	{
		command->execute();
		_executedCommands.push_back(command);
		_undoneCommands.clear(); // 清除重做命令列表
	}

	void undo() 
	{
		if (!_executedCommands.empty()) 
		{
			Command* lastCommand = _executedCommands.back();
			lastCommand->undo();
			_undoneCommands.push_back(lastCommand);
			_executedCommands.pop_back();
		}
	}

	void redo() 
	{
		if (!_undoneCommands.empty()) 
		{
			Command* lastCommand = _undoneCommands.back();
			lastCommand->execute();
			_executedCommands.push_back(lastCommand);
			_undoneCommands.pop_back();
		}
	}
};


int main()
{
	int value = 10;
	CommandManager manager;

	// 增加 5
	manager.execute(new IncreaseCommand(&value, 5));
	std::cout << "Value: " << value << std::endl; // 输出: 15

	// 减少 3
	manager.execute(new DecreaseCommand(&value, 3));
	std::cout << "Value: " << value << std::endl; // 输出: 12

	// 撤销
	manager.undo();
	std::cout << "Value: " << value << std::endl; // 输出: 15

	// 重做
	manager.redo();
	std::cout << "Value: " << value << std::endl; // 输出: 12

	return 0;
}