求一个qtcreator编写的命令模式的代码框架,要求有8个命令
时间: 2024-03-13 11:47:57 浏览: 47
好的,这是一个基本的命令模式的代码框架,包括8个命令:
```cpp
#include <iostream>
#include <vector>
class Command {
public:
virtual ~Command() {}
virtual void execute() = 0;
};
class Receiver {
public:
void action1() {
std::cout << "Receiver: 执行命令1" << std::endl;
}
void action2() {
std::cout << "Receiver: 执行命令2" << std::endl;
}
// 其他命令的实现
};
class Command1 : public Command {
public:
Command1(Receiver* receiver) : receiver_(receiver) {}
void execute() override {
receiver_->action1();
}
private:
Receiver* receiver_;
};
class Command2 : public Command {
public:
Command2(Receiver* receiver) : receiver_(receiver) {}
void execute() override {
receiver_->action2();
}
private:
Receiver* receiver_;
};
// 其他命令类的实现
class Invoker {
public:
void set_command(Command* command) {
command_ = command;
}
void execute_command() {
if (command_) {
command_->execute();
}
}
private:
Command* command_;
};
int main() {
Receiver* receiver = new Receiver;
Command1* command1 = new Command1(receiver);
Command2* command2 = new Command2(receiver);
// 其他命令的实例化
Invoker invoker;
invoker.set_command(command1);
invoker.execute_command();
invoker.set_command(command2);
invoker.execute_command();
// 执行其他命令
delete receiver;
delete command1;
delete command2;
// 删除其他命令
return 0;
}
```
请注意,这只是一个基本的框架,您需要根据您的具体需求进行修改和扩展。
阅读全文