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

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

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

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

代码实现:

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

cpp 复制代码
#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)

功能扩展:

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

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

相关推荐
贱贱的剑3 小时前
2.单例模式
单例模式·设计模式
Your易元5 小时前
设计模式-模板方法模式
java·设计模式·模板方法模式
暴走的海鸽8 小时前
存储库模式赋能 Django:让你的代码不那么业余,更具生命力
python·设计模式·django
小张在编程9 小时前
Java设计模式实战:备忘录模式与状态机模式的“状态管理”双雄
java·设计模式·备忘录模式
小小寂寞的城1 天前
JAVA观察者模式demo【设计模式系列】
java·观察者模式·设计模式
花好月圆春祺夏安1 天前
基于odoo17的设计模式详解---备忘模式
数据库·设计模式
DKPT1 天前
Java设计模式之行为型模式(责任链模式)介绍与说明
java·笔记·学习·观察者模式·设计模式
使一颗心免于哀伤1 天前
《设计模式之禅》笔记摘录 - 6.原型模式
笔记·设计模式
ffcf2 天前
设计模式—专栏简介
设计模式
tianchang2 天前
SSR 深度解析:从原理到实践的完整指南
前端·vue.js·设计模式