以绘图(绘制点、直线、圆、椭圆、多段线)为例子 通过设计模式中的命令模式实现

为了在命令模式的基础上实现撤销(Undo)和回退(Redo)功能,我们可以在每个命令类中记录一些必要的状态,允许我们撤销之前的操作,并在需要时回退操作。常见的做法是使用一个命令堆栈来存储历史命令,并为每个命令提供撤销(undo)操作。

我们可以通过以下步骤来添加撤销和回退功能:

  1. Command接口:为命令接口添加一个undo()方法。
  2. 具体命令类(ConcreteCommand):为每个具体命令类实现undo()方法,撤销相应的操作。
  3. Invoker类:管理一个命令堆栈(历史记录),并实现undo()redo()方法来执行撤销和回退操作。

代码实现:

我们将对之前的代码进行修改,来实现撤销和回退功能:

#include <iostream>
#include <memory>
#include <vector>
#include <stack>// 绘图命令接口
class Command {
public:virtual ~Command() = default;virtual void execute() = 0;virtual void undo() = 0;  // 增加撤销操作接口
};// 接收者:绘图工具(如画布)
class Receiver {
public:void drawPoint(int x, int y) {std::cout << "Drawing point at (" << x << ", " << y << ").\n";}void undoDrawPoint(int x, int y) {std::cout << "Undo drawing point at (" << x << ", " << y << ").\n";}void drawLine(int x1, int y1, int x2, int y2) {std::cout << "Drawing line from (" << x1 << ", " << y1 << ") to (" << x2 << ", " << y2 << ").\n";}void undoDrawLine(int x1, int y1, int x2, int y2) {std::cout << "Undo drawing line from (" << x1 << ", " << y1 << ") to (" << x2 << ", " << y2 << ").\n";}void drawCircle(int x, int y, int radius) {std::cout << "Drawing circle at (" << x << ", " << y << ") with radius " << radius << ".\n";}void undoDrawCircle(int x, int y, int radius) {std::cout << "Undo drawing circle at (" << x << ", " << y << ") with radius " << radius << ".\n";}void drawEllipse(int x, int y, int majorAxis, int minorAxis) {std::cout << "Drawing ellipse at (" << x << ", " << y << ") with major axis " << majorAxis << " and minor axis " << minorAxis << ".\n";}void undoDrawEllipse(int x, int y, int majorAxis, int minorAxis) {std::cout << "Undo drawing ellipse at (" << x << ", " << y << ") with major axis " << majorAxis << " and minor axis " << minorAxis << ".\n";}void drawPolyline(const std::vector<std::pair<int, int>>& points) {std::cout << "Drawing polyline with the following points:\n";for (const auto& point : points) {std::cout << "(" << point.first << ", " << point.second << ") ";}std::cout << "\n";}void undoDrawPolyline(const std::vector<std::pair<int, int>>& points) {std::cout << "Undo drawing polyline with the following points:\n";for (const auto& point : points) {std::cout << "(" << point.first << ", " << point.second << ") ";}std::cout << "\n";}
};// 绘制点的命令
class DrawPointCommand : public Command {
private:Receiver* receiver;int x, y;
public:DrawPointCommand(Receiver* r, int x, int y) : receiver(r), x(x), y(y) {}void execute() override {receiver->drawPoint(x, y);}void undo() override {receiver->undoDrawPoint(x, y);}
};// 绘制直线的命令
class DrawLineCommand : public Command {
private:Receiver* receiver;int x1, y1, x2, y2;
public:DrawLineCommand(Receiver* r, int x1, int y1, int x2, int y2) : receiver(r), x1(x1), y1(y1), x2(x2), y2(y2) {}void execute() override {receiver->drawLine(x1, y1, x2, y2);}void undo() override {receiver->undoDrawLine(x1, y1, x2, y2);}
};// 绘制圆形的命令
class DrawCircleCommand : public Command {
private:Receiver* receiver;int x, y, radius;
public:DrawCircleCommand(Receiver* r, int x, int y, int radius) : receiver(r), x(x), y(y), radius(radius) {}void execute() override {receiver->drawCircle(x, y, radius);}void undo() override {receiver->undoDrawCircle(x, y, radius);}
};// 绘制椭圆的命令
class DrawEllipseCommand : public Command {
private:Receiver* receiver;int x, y, majorAxis, minorAxis;
public:DrawEllipseCommand(Receiver* r, int x, int y, int majorAxis, int minorAxis) : receiver(r), x(x), y(y), majorAxis(majorAxis), minorAxis(minorAxis) {}void execute() override {receiver->drawEllipse(x, y, majorAxis, minorAxis);}void undo() override {receiver->undoDrawEllipse(x, y, majorAxis, minorAxis);}
};// 绘制多段线的命令
class DrawPolylineCommand : public Command {
private:Receiver* receiver;std::vector<std::pair<int, int>> points;
public:DrawPolylineCommand(Receiver* r, const std::vector<std::pair<int, int>>& points) : receiver(r), points(points) {}void execute() override {receiver->drawPolyline(points);}void undo() override {receiver->undoDrawPolyline(points);}
};// 调用者:工具栏或按钮
class Invoker {
private:std::stack<std::shared_ptr<Command>> commandHistory;  // 历史命令栈std::stack<std::shared_ptr<Command>> redoStack;      // 重做命令栈public:void executeCommand(std::shared_ptr<Command> cmd) {cmd->execute();commandHistory.push(cmd);  // 将命令压入历史栈while (!redoStack.empty()) {  // 清空重做栈redoStack.pop();}}void undo() {if (!commandHistory.empty()) {std::shared_ptr<Command> cmd = commandHistory.top();commandHistory.pop();cmd->undo();redoStack.push(cmd);  // 将撤销的命令压入重做栈} else {std::cout << "No command to undo.\n";}}void redo() {if (!redoStack.empty()) {std::shared_ptr<Command> cmd = redoStack.top();redoStack.pop();cmd->execute();commandHistory.push(cmd);  // 将重做的命令压入历史栈} else {std::cout << "No command to redo.\n";}}
};// 客户端代码
int main() {Receiver receiver;  // 绘图工具(画布)// 创建具体的命令std::shared_ptr<Command> drawPoint = std::make_shared<DrawPointCommand>(&receiver, 10, 20);std::shared_ptr<Command> drawLine = std::make_shared<DrawLineCommand>(&receiver, 10, 20, 30, 40);std::shared_ptr<Command> drawCircle = std::make_shared<DrawCircleCommand>(&receiver, 50, 50, 15);std::shared_ptr<Command> drawEllipse = std::make_shared<DrawEllipseCommand>(&receiver, 70, 70, 20, 10);std::vector<std::pair<int, int>> polylinePoints = {{10, 10}, {20, 20}, {30, 30}, {40, 40}};std::shared_ptr<Command> drawPolyline = std::make_shared<DrawPolylineCommand>(&receiver, polylinePoints);// 创建调用者Invoker invoker;// 模拟用户操作,通过调用命令绘制图形invoker.executeCommand(drawPoint);invoker.executeCommand(drawLine);invoker.executeCommand(drawCircle);invoker.executeCommand(drawEllipse);invoker.executeCommand(drawPolyline);// 撤销操作std::cout << "\nUndoing the last command:\n";invoker.undo();// 回退(重做)操作std::cout << "\nRedoing the last undone command:\n";invoker.redo();return 0;
}

关键修改:

  1. Command接口:添加了undo()方法,使每个命令都能撤销其操作。
  2. Receiver类:为每个绘制方法添加了撤销方法(undoDraw...),用于撤销具体的图形操作。
  3. Invoker类:管理两个栈——commandHistory(历史命令栈)和redoStack(重做命令栈)。在执行命令时将其压入commandHistory,在撤销时将命令从commandHistory中取出并执行undo(),同时将命令压入redoStack。回退时从redoStack取出命令并重新执行。

输出:

Drawing point at (10, 20).
Drawing line from (10, 20) to (30, 40).
Drawing circle at (50, 50) with radius 15.
Drawing ellipse at (70, 70) with major axis 20 and minor axis 10.
Drawing polyline with the following points:
(10, 10) (20, 20) (30, 30) (40, 40)Undoing the last command:
Undo drawing polyline with the following points:
(10, 10) (20, 20) (30, 30) (40, 40)Redoing the last undone command:
Drawing polyline with the following points:
(10, 10) (20, 20) (30, 30) (40, 40)

功能扩展:

  • 撤销操作:允许撤销最后的绘图命令。
  • 回退操作:允许重做之前撤销的命令。

这样,我们就实现了撤销和回退功能,用户可以随时撤销之前的操作并恢复它们。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/896339.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

git从本地其他设备上fetch分支

在 Git 中&#xff0c;如果你想从本地其他设备上获取分支&#xff0c;可以通过以下几种方式实现。不过&#xff0c;需要注意的是&#xff0c;Git 本身是分布式版本控制系统&#xff0c;通常我们是从远程仓库&#xff08;如 GitHub、GitLab 等&#xff09;拉取分支&#xff0c;而…

故障诊断 | Matlab实现基于DBO-BP-Bagging多特征分类预测/故障诊断

故障诊断 | Matlab实现基于DBO-BP-Bagging多特征分类预测/故障诊断 目录 故障诊断 | Matlab实现基于DBO-BP-Bagging多特征分类预测/故障诊断分类效果基本介绍模型描述DBO-BP-Bagging蜣螂算法优化多特征分类预测一、引言1.1、研究背景和意义1.2、研究现状1.3、研究目的与方法 二…

CentOS停服后的替代选择:openEuler、Rocky Linux及其他系统的未来展望

CentOS停服后的替代选择&#xff1a;openEuler、Rocky Linux及其他系统的未来展望 引言CentOS停服的背景华为openEuler&#xff1a;面向未来的开源操作系统1. 简介2. 特点3. 发展趋势 Rocky Linux&#xff1a;CentOS的精神继承者1. 简介2. 特点3. 发展趋势 其他可选的替代系统1…

docker部署go简单web项目(无mysql等附加功能)

首先准备好go语言代码 代码表示当访问主机上8080端口下的/hello路径时&#xff0c;会返回hello&#xff0c;world。 package mainimport ("fmt""github.com/gin-gonic/gin" )type hh struct {S string }func main() {router : gin.Default()router.GET(&…

OceanBase数据库实战:Windows Docker部署与DBeaver无缝对接

一、前言 OceanBase 是一款高性能、高可扩展的分布式数据库&#xff0c;适用于大规模数据处理和企业级应用。 随着大数据和云计算的普及&#xff0c;OceanBase 在企业数字化转型中扮演着重要角色。学习 OceanBase 可以帮助开发者掌握先进的分布式数据库技术&#xff0c;提升数…

在 HuggingFace 中使用 SSH 进行下载数据集和模型

SSH 是一种 安全通讯的协议&#xff0c;我们通过配置 SSH 的密钥 来在 Git 上实现 Huggingface 模型的命令行下载。 参考网址&#xff1a;https://huggingface.co/docs/hub/security-git-ssh 点击自己的头像&#xff0c;点击 Add SSH key 在 Windows 上&#xff0c;我们实现已…

.NET Core MVC IHttpActionResult 设置Headers

最近碰到调用我的方法要求返回一个代码值&#xff0c;但是要求是不放在返回实体里&#xff0c;而是放在返回的Headers上 本来返回我是直接用 return Json(res) 这种封装的方法特别简单&#xff0c;但是没有发现设置headers的地方 查询过之后不得已换了个返回 //原来方式 //…

Linux-----进程间通信

一、按通信范围分类 同一主机进程通信 传统IPC方式&#xff1a; 管道&#xff08;无名管道、有名管道&#xff09;信号&#xff08;Signal&#xff09; System V IPC&#xff1a; 共享内存&#xff08;效率最高&#xff09;消息队列信号量 POSIX IPC&#xff08;较新标准&#…

Ant Design按钮样式深度适配:实现<Button>与<a>标签颜色完美同步

Ant Design按钮样式深度适配&#xff1a;实现与标签颜色完美同步 问题现象诊断 组件结构原型 <Button type"link" disabled{disabled}><a href"...">下载</a> </Button>样式冲突表现 状态按钮颜色链接颜色视觉问题启用态Ant蓝…

使用Python爬虫获取淘宝商品详情:API返回值说明与案例指南

在电商数据分析和运营中&#xff0c;获取淘宝商品详情是常见的需求。淘宝开放平台提供了丰富的API接口&#xff0c;允许开发者通过合法的方式获取商品信息。本文将详细介绍如何使用Python编写爬虫&#xff0c;通过淘宝API获取商品详情&#xff0c;并解析API返回值的含义和结构。…

并发 -- 无锁算法与结构

文章目录 什么是无锁算法什么是原子变量什么是CAS操作Compare-And-Swap Weak在哪些情况下会失败举例说明无锁结构无锁结构的问题 什么是无锁算法 无锁算法&#xff08;Lock-Free Algorithm&#xff09;是一种并发编程技术&#xff0c;旨在实现多线程环境下的高效数据共享&…

考研/保研复试英语问答题库(华工建院)

华南理工大学建筑学院保研/考研 英语复试题库&#xff0c;由华工保研er和学硕笔试第一同学一起整理&#xff0c;覆盖面广&#xff0c;助力考研/保研上岸&#xff01;需要&#x1f447;载可到文章末尾见小&#x1f360;。 以下是主要内容&#xff1a; Part0 复试英语的方法论 Pa…

岳阳市美术馆预约平台(小程序论文源码调试讲解)

第4章 系统设计 一个成功设计的系统在内容上必定是丰富的&#xff0c;在系统外观或系统功能上必定是对用户友好的。所以为了提升系统的价值&#xff0c;吸引更多的访问者访问系统&#xff0c;以及让来访用户可以花费更多时间停留在系统上&#xff0c;则表明该系统设计得比较专…

Python游戏编程之赛车游戏6-3

1 “敌人”汽车类的创建 在创建玩家汽车类之后&#xff0c;接下来创建“敌人”汽车类。“敌人”汽车类与玩家类一样&#xff0c;也是包含两个方法&#xff0c;一个是__init__()&#xff0c;另一个是move()。 1.1 __init__()方法 “敌人”汽车类的__init__()方法代码如图1所示…

TCP/UDP调试工具推荐:Socket通信图解教程

TCP/UDP调试工具推荐&#xff1a;Socket通信图解教程 一、引言二、串口调试流程三、下载链接 SocketTool 调试助手是一款旨在协助程序员和网络管理员进行TCP和UDP协议调试的网络通信工具。TCP作为一种面向连接、可靠的协议&#xff0c;具有诸如连接管理、数据分片与重组、流量和…

神经网络 - 神经元

人工神经元(Artificial Neuron)&#xff0c;简称神经元(Neuron)&#xff0c;是构成神经网络的基本单元&#xff0c;其主要是模拟生物神经元的结构和特性&#xff0c;接收一组输入信号并产生输出。 生物学家在 20 世纪初就发现了生物神经元的结构。一个生物神经元通常具有多个树…

蓝桥杯备考:贪心算法之矩阵消除游戏

这道题是牛客上的一道题&#xff0c;它呢和我们之前的排座位游戏非常之相似&#xff0c;但是&#xff0c;排座位问题选择行和列是不会改变元素的值的&#xff0c;这道题呢每每选一行都会把这行或者这列清零&#xff0c;所以我们的策略就是先用二进制把选择所有行的情况全部枚举…

DeepSeek系统架构的逐层分类拆解分析,从底层基础设施到用户端分发全链路

一、底层基础设施层 1. 硬件服务器集群 算力单元&#xff1a; GPU集群&#xff1a;基于NVIDIA H800/H100 GPU构建&#xff0c;单集群规模超10,000卡&#xff0c;采用NVLink全互联架构实现低延迟通信。国产化支持&#xff1a;适配海光DCU、寒武纪MLU等国产芯片&#xff0c;通过…

ktransformers 上的 DeepSeek-R1 671B open-webui

ktransformers 上的 DeepSeek-R1 671B open-webui 一、下载GGUF模型1. 创建目录2. 魔塔下载 DeepSeek-R1-Q4_K_M3. 安装显卡驱动和cuda4. 显卡 NVIDIA GeForce RTX 4090 二、安装ktransformers1. 安装依赖2. 安装uv工具链3. 下载源码4. 创建python虚拟环境 三、编译ktransforme…

smolagents学习笔记系列(五)Tools-in-depth-guide

这篇文章锁定官网教程中的 Tools-in-depth-guide 章节&#xff0c;主要介绍了如何详细构造自己的Tools&#xff0c;在之前的博文 smolagents学习笔记系列&#xff08;二&#xff09;Agents - Guided tour 中我初步介绍了下如何将一个函数或一个类声明成 smolagents 的工具&…