1 Star 1 Fork 0

子安/linux-sys

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
command_pattern.cpp 2.61 KB
一键复制 编辑 原始数据 按行查看 历史
子安 提交于 2020-11-29 22:40 +08:00 . 增加剩余的23种设计模式
//
// Created by andrew on 2020/11/23.
//
#include <iostream>
#include <list>
using namespace std;
class Doctor {
public:
void treatEye() {
cout << "treat eye" << endl;
}
void treatNose() {
cout << "treat nose" << endl;
}
};
class Command {
public:
virtual void treat() = 0;
virtual ~Command() = default;
};
class CommandTreatEye : public Command {
public:
explicit CommandTreatEye(Doctor *doctor) {
m_doctor = doctor;
}
void treat() override {
m_doctor->treatEye();
}
private:
Doctor *m_doctor;
};
class CommandTreatNose : public Command {
public:
explicit CommandTreatNose(Doctor *doctor) {
m_doctor = doctor;
}
void treat() override {
m_doctor->treatNose();
}
private:
Doctor *m_doctor;
};
//小护士
class BeautyNurse {
public:
explicit BeautyNurse(Command *command) {
this->command = command;
}
void SubmittedCase() { // 提交病历 下单命令
command->treat();
}
private:
Command *command;
};
class HeadNurse {
public:
HeadNurse() {
m_list.clear();
}
void setCommand(Command *command) {
m_list.push_back(command);
}
void SubmittedCase() { // 提交命令
for (auto & it : m_list) {
it->treat();
}
}
private:
list<Command *> m_list;
};
void nurseCommand() {
BeautyNurse *beautyNurse = nullptr;
Doctor *doctor = nullptr;
Command *command = nullptr;
doctor = new Doctor;
//
command = new CommandTreatNose(doctor);
beautyNurse = new BeautyNurse(command);
beautyNurse->SubmittedCase();
delete doctor;
delete command;
delete beautyNurse;
}
void headNurseCommand() {
// 护士长 提交病历 给以上看病
HeadNurse *headNurse = NULL;
Doctor * doctor = NULL;
Command *command1 = NULL;
Command *command2 = NULL;
doctor = new Doctor;
command1 = new CommandTreatEye(doctor);
command2 = new CommandTreatNose(doctor);
headNurse = new HeadNurse;
headNurse->setCommand(command1);
headNurse->setCommand(command2);
headNurse->SubmittedCase(); // 护士长 批量下单命令
delete doctor;
delete command1;
delete command2;
delete headNurse;
}
int main(int argc, char *argv[]) {
// 通过一个命令 调用医生实现病的治疗 命令的执行部分实现
/* Doctor *dcotor = new Doctor;
CommandTreatEye *commandTreatEye = new CommandTreatEye(dcotor);
commandTreatEye->treat();
delete commandTreatEye;
delete dcotor;*/
//
nurseCommand();
headNurseCommand();
return 0;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/andrewgithub/linux-sys.git
git@gitee.com:andrewgithub/linux-sys.git
andrewgithub
linux-sys
linux-sys
dfew

搜索帮助