• 【设计模式】命令模式


    概念

    行为模式


    类图

    Command


    代码

    #include <iostream>
    #include <stack>
    
    using namespace std;
    
    class Editor {
    public:
        string GetSelection() { return text; };
        void DeleteSelection() { text = ""; };
        void ReplaceSelection(const string& t) { text = t; };
    
    private:
        string text;
    };
    
    class Application;
    class Command {
    public:
        Command(Application* app, Editor* editor) {
            this->app = app;
            this->editor = editor;
        }
    
        void SaveBackup() {
            backup = editor->GetSelection();
        }
    
        void Undo() {
            editor->ReplaceSelection(backup);
        }
    
        virtual bool Execute() = 0;
    
    protected:
        Application* app;
        Editor* editor;
        string backup;
    };
    
    class CopyCommand : public Command {
    public:
        bool Execute() override {
            SaveBackup();
            // app->clipboard = editor->GetSelection();
            return false;
        }
    };
    
    class CutCommand : public Command {
    public:
        bool Execute() override {
            SaveBackup();
            // app->clipboard = editor->GetSelection();
            editor->DeleteSelection();
            return true;
        }
    };
    
    class PasteCommand : public Command {
    public:
        bool Execute() override {
            SaveBackup();
            // editor->ReplaceSelection(app->clipboard);
            return true;
        }
    };
    
    class UndoCommand : public Command {
    public:
        bool Execute() override {
            // app->Undo();
            return false;
        }
    };
    
    class CommandHistory {
    public:
        void Push(Command* cmd) {
            history.push(cmd);
        }
    
        Command* Pop() {
            auto cmd = history.top();
            history.pop();
            return cmd;
        }
    
    private:
        stack<Command*> history;
    };
    
    class Application {
        // omit cause one cpp file
    };
    
    int main(int argc, char *argv[]) {
        cout << "Command pattern needs to be complemented." << endl;
        cout << "One cpp file cannot satisfy." << endl;
    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
  • 相关阅读:
    家用电器行业商业供应链协同平台解决方案:供应链系统管理精益化,助推企业智造升级
    ADB 命令结合 Monkey 的使用
    Vue中使用VueAMap
    【Axure高保真原型】3D环形图_移入显示数据标签
    如何在数据库中存储小数:FLOAT、DECIMAL还是BIGINT?
    ZooKeeper 避坑实践:如何调优 jute.maxbuffer
    Java实验报告(三)
    JWT
    Mysql高阶语句(二)
    图神经网络(四):GCN
  • 原文地址:https://blog.csdn.net/phdsky/article/details/125531352