当前位置: 首页 > news >正文

外贸公司网站源码广州推广系统

外贸公司网站源码,广州推广系统,网页布局的目的,wordpress 301怎么写为了在命令模式的基础上实现撤销(Undo)和回退(Redo)功能,我们可以在每个命令类中记录一些必要的状态,允许我们撤销之前的操作,并在需要时回退操作。常见的做法是使用一个命令堆栈来存储历史命令…

为了在命令模式的基础上实现撤销(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.zhongyajixie.com/news/44590.html

相关文章:

  • 淮南公司做网站百度指数有什么参考意义
  • 做站群网站好优化吗百度2022第三季度财报
  • 做数据分析网站如何开发微信小程序
  • 做网站的开发心得大连网络营销seo
  • 网站做用户登录seo线上培训机构
  • wordpress登录后才能下载seo公司哪家好
  • 新建网站怎么保存如何写软文
  • 做网站申请哪类商标怎么分析一个网站seo
  • 做网站的接口是意思可以推广的软件
  • 网站流量限制怎么制作网站平台
  • 免费的网站域名查询方法有哪些百度ai营销中国行
  • 郑州做网站网站建设费用优化大师官方下载
  • 做pc端网站方案成都专业的整站优化
  • 网站开发需要什么设备大众网疫情最新消息
  • 哪个网站做刷手最好seo专员很难吗
  • 有哪些网站是免费学做网页的西安网站公司推广
  • 个人域名怎么做社交网站seo大牛
  • 网站推广广告营销方案app推广渠道
  • 网站排名优化外包公司百度seo收录软件
  • wordpress好用的排版seo日常工作内容
  • 晋州网站建设外贸网站建设推广公司
  • 国外免费个人网站空间软文发稿系统
  • 企业建设营销型网站步骤可靠的网站优化
  • 江津网站建设江北seo综合优化外包
  • 做名片网站赣州是哪个省
  • 深圳企业网站制作报价河南百度推广公司
  • 做的比较好的设计公司网站十大销售管理软件排行榜
  • 礼品公司怎么做网站seo快速排名软件
  • 免费毕业设计的网站建设什么是网络营销策略
  • wordpress 清爽主题seo网络优化是什么工作