• C++中的命令模式


    目录

    命令模式(Command Pattern)

    实际应用

    家庭自动化系统

    文件操作系统

    总结


    命令模式(Command Pattern)

    命令模式是一种行为型设计模式,它将一个请求封装成一个对象,从而使您可以用不同的请求对客户端进行参数化、对请求排队或记录请求日志,以及支持可撤销的操作。命令模式将请求的发出者和执行者解耦,使得请求的发送者无需知道执行请求的具体操作。

    实际应用

    家庭自动化系统

    命令模式来实现控制各种设备(如灯、电视、空调)。

    1. #include
    2. #include
    3. #include
    4. // 命令接口
    5. class Command {
    6. public:
    7. virtual ~Command() = default;
    8. virtual void execute() = 0;
    9. };
    10. // 接收者:灯
    11. class Light {
    12. public:
    13. void on() {
    14. std::cout << "Light is ON\n";
    15. }
    16. void off() {
    17. std::cout << "Light is OFF\n";
    18. }
    19. };
    20. // 接收者:电视
    21. class TV {
    22. public:
    23. void on() {
    24. std::cout << "TV is ON\n";
    25. }
    26. void off() {
    27. std::cout << "TV is OFF\n";
    28. }
    29. };
    30. // 接收者:空调
    31. class AirConditioner {
    32. public:
    33. void on() {
    34. std::cout << "AirConditioner is ON\n";
    35. }
    36. void off() {
    37. std::cout << "AirConditioner is OFF\n";
    38. }
    39. };
    40. // 具体命令:打开灯
    41. class LightOnCommand : public Command {
    42. private:
    43. Light& light;
    44. public:
    45. LightOnCommand(Light& light) : light(light) {}
    46. void execute() override {
    47. light.on();
    48. }
    49. };
    50. // 具体命令:关闭灯
    51. class LightOffCommand : public Command {
    52. private:
    53. Light& light;
    54. public:
    55. LightOffCommand(Light& light) : light(light) {}
    56. void execute() override {
    57. light.off();
    58. }
    59. };
    60. // 具体命令:打开电视
    61. class TVOnCommand : public Command {
    62. private:
    63. TV& tv;
    64. public:
    65. TVOnCommand(TV& tv) : tv(tv) {}
    66. void execute() override {
    67. tv.on();
    68. }
    69. };
    70. // 具体命令:关闭电视
    71. class TVOffCommand : public Command {
    72. private:
    73. TV& tv;
    74. public:
    75. TVOffCommand(TV& tv) : tv(tv) {}
    76. void execute() override {
    77. tv.off();
    78. }
    79. };
    80. // 具体命令:打开空调
    81. class AirConditionerOnCommand : public Command {
    82. private:
    83. AirConditioner& airConditioner;
    84. public:
    85. AirConditionerOnCommand(AirConditioner& airConditioner) : airConditioner(airConditioner) {}
    86. void execute() override {
    87. airConditioner.on();
    88. }
    89. };
    90. // 具体命令:关闭空调
    91. class AirConditionerOffCommand : public Command {
    92. private:
    93. AirConditioner& airConditioner;
    94. public:
    95. AirConditionerOffCommand(AirConditioner& airConditioner) : airConditioner(airConditioner) {}
    96. void execute() override {
    97. airConditioner.off();
    98. }
    99. };
    100. // 遥控器
    101. class RemoteControl {
    102. private:
    103. std::vector> onCommands;
    104. std::vector> offCommands;
    105. public:
    106. void setCommand(size_t slot, std::shared_ptr onCommand, std::shared_ptr offCommand) {
    107. if (slot >= onCommands.size()) {
    108. onCommands.resize(slot + 1);
    109. offCommands.resize(slot + 1);
    110. }
    111. onCommands[slot] = onCommand;
    112. offCommands[slot] = offCommand;
    113. }
    114. void onButtonPressed(size_t slot) {
    115. if (slot < onCommands.size() && onCommands[slot]) {
    116. onCommands[slot]->execute();
    117. }
    118. }
    119. void offButtonPressed(size_t slot) {
    120. if (slot < offCommands.size() && offCommands[slot]) {
    121. offCommands[slot]->execute();
    122. }
    123. }
    124. };
    125. int main() {
    126. Light livingRoomLight;
    127. TV livingRoomTV;
    128. AirConditioner livingRoomAC;
    129. auto livingRoomLightOn = std::make_shared(livingRoomLight);
    130. auto livingRoomLightOff = std::make_shared(livingRoomLight);
    131. auto livingRoomTVOn = std::make_shared(livingRoomTV);
    132. auto livingRoomTVOff = std::make_shared(livingRoomTV);
    133. auto livingRoomACOn = std::make_shared(livingRoomAC);
    134. auto livingRoomACOff = std::make_shared(livingRoomAC);
    135. RemoteControl remote;
    136. remote.setCommand(0, livingRoomLightOn, livingRoomLightOff);
    137. remote.setCommand(1, livingRoomTVOn, livingRoomTVOff);
    138. remote.setCommand(2, livingRoomACOn, livingRoomACOff);
    139. remote.onButtonPressed(0);
    140. remote.offButtonPressed(0);
    141. remote.onButtonPressed(1);
    142. remote.offButtonPressed(1);
    143. remote.onButtonPressed(2);
    144. remote.offButtonPressed(2);
    145. return 0;
    146. }

    文件操作系统

    用命令模式来执行一系列文件操作(如创建、删除、重命名文件)。

    1. #include
    2. #include
    3. #include
    4. #include
    5. // 命令接口
    6. class Command {
    7. public:
    8. virtual ~Command() = default;
    9. virtual void execute() = 0;
    10. virtual void undo() = 0;
    11. };
    12. // 接收者:文件系统
    13. class FileSystem {
    14. public:
    15. void createFile(const std::string& filename) {
    16. std::cout << "File created: " << filename << "\n";
    17. }
    18. void deleteFile(const std::string& filename) {
    19. std::cout << "File deleted: " << filename << "\n";
    20. }
    21. void renameFile(const std::string& oldName, const std::string& newName) {
    22. std::cout << "File renamed from " << oldName << " to " << newName << "\n";
    23. }
    24. };
    25. // 具体命令:创建文件
    26. class CreateFileCommand : public Command {
    27. private:
    28. FileSystem& fileSystem;
    29. std::string filename;
    30. public:
    31. CreateFileCommand(FileSystem& fs, const std::string& filename) : fileSystem(fs), filename(filename) {}
    32. void execute() override {
    33. fileSystem.createFile(filename);
    34. }
    35. void undo() override {
    36. fileSystem.deleteFile(filename);
    37. }
    38. };
    39. // 具体命令:删除文件
    40. class DeleteFileCommand : public Command {
    41. private:
    42. FileSystem& fileSystem;
    43. std::string filename;
    44. public:
    45. DeleteFileCommand(FileSystem& fs, const std::string& filename) : fileSystem(fs), filename(filename) {}
    46. void execute() override {
    47. fileSystem.deleteFile(filename);
    48. }
    49. void undo() override {
    50. fileSystem.createFile(filename);
    51. }
    52. };
    53. // 具体命令:重命名文件
    54. class RenameFileCommand : public Command {
    55. private:
    56. FileSystem& fileSystem;
    57. std::string oldName;
    58. std::string newName;
    59. public:
    60. RenameFileCommand(FileSystem& fs, const std::string& oldName, const std::string& newName)
    61. : fileSystem(fs), oldName(oldName), newName(newName) {}
    62. void execute() override {
    63. fileSystem.renameFile(oldName, newName);
    64. }
    65. void undo() override {
    66. fileSystem.renameFile(newName, oldName);
    67. }
    68. };
    69. // 命令管理器
    70. class CommandManager {
    71. private:
    72. std::stack> commandStack;
    73. std::stack> redoStack;
    74. public:
    75. void executeCommand(std::shared_ptr command) {
    76. command->execute();
    77. commandStack.push(command);
    78. while (!redoStack.empty()) {
    79. redoStack.pop();
    80. }
    81. }
    82. void undo() {
    83. if (!commandStack.empty()) {
    84. auto command = commandStack.top();
    85. commandStack.pop();
    86. command->undo();
    87. redoStack.push(command);
    88. }
    89. }
    90. void redo() {
    91. if (!redoStack.empty()) {
    92. auto command = redoStack.top();
    93. redoStack.pop();
    94. command->execute();
    95. commandStack.push(command);
    96. }
    97. }
    98. };
    99. int main() {
    100. FileSystem fileSystem;
    101. auto createFile = std::make_shared(fileSystem, "example.txt");
    102. auto deleteFile = std::make_shared(fileSystem, "example.txt");
    103. auto renameFile = std::make_shared(fileSystem, "example.txt", "example1.txt");
    104. CommandManager commandManager;
    105. commandManager.executeCommand(createFile);
    106. commandManager.executeCommand(renameFile);
    107. commandManager.undo();
    108. commandManager.redo();
    109. commandManager.executeCommand(deleteFile);
    110. commandManager.undo();
    111. return 0;
    112. }

    总结

    命令模式在开发中可以帮助我们将请求封装为对象,并提供对请求执行、撤销和重做的支持。

  • 相关阅读:
    计算机与操作系统
    PyTorch多GPU训练模型——使用单GPU或CPU进行推理的方法
    K8S 性能优化 - OS sysctl 调优
    C# 主程序调用其他的Exe程序后,怎么获取其他程序的输出内容
    canvas 学习
    5.MySQL高级语句,你给我学!
    第五章、实现Web适配器
    【每日一题】1041. 困于环中的机器人
    协程Part1-boost.Coroutine.md
    nlp序列完全可以模拟人脑智能
  • 原文地址:https://blog.csdn.net/GOLOJO/article/details/139604973