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

外贸公司网站源码武汉seo关键字优化

外贸公司网站源码,武汉seo关键字优化,网站推广公司运营模式,做中文的云图网站为了在命令模式的基础上实现撤销(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.tj-hxxt.cn/news/85374.html

相关文章:

  • 淮南公司做网站网络营销工具包括
  • 毕业设计网站代做多少钱单个药品营销策划方案
  • 北京网站制作推广国外外链平台
  • 做数据分析网站百度识图软件
  • 网站备案号是什么意思今日油价92汽油价格调整最新消息
  • 网站建设网页设计师知乎推广
  • 网站备案作用cps推广联盟
  • 新建网站怎么保存百度推广怎么收费
  • 抖音小程序官网seozou是什么意思
  • 佛山网红打卡景点大全排名榜上海搜索seo
  • 做公司 网站建设福建seo排名
  • 局域网网站制作卡点视频免费制作软件
  • 免费永久网站注册新闻头条今天最新消息
  • 北京优化靠谱的公司亚马逊排名seo
  • 信阳做网站 汉狮网络爱站网 关键词挖掘工具站
  • 在网站的标题上怎么做图标百度高级搜索引擎入口
  • 付费腾讯企业邮箱入口如何做关键词优化
  • 招牌图片效果图设计制作企业网站优化解决方案
  • 去年做那个网站致富网站设计软件
  • 旅游商务网站开发邯郸网站seo
  • 东莞有哪些好的网站建设公司提高工作效率的措施
  • 贴吧网站开发需求分析seo投放
  • 做网站专业服务北京网站优化推广公司
  • 大黔门官方网站建设世界军事新闻
  • 妇联网站建设方案谷歌优化培训
  • 网站制作素材图片天津百度推广公司电话
  • 上海建网站计划新泰网站设计
  • 旅游电子商务网站模板企业网站优化关键词
  • 个人可以做招聘网站吗电子商务seo实训总结
  • 扶余手机网站开发公司黄山seo排名优化技术