#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:
Command(Doctor*doctor)
{
m_doctor = doctor;
}
virtual void treat() = 0;
protected:
Doctor*m_doctor;
};
class CommandTreat_Eye:public Command
{
public:
CommandTreat_Eye(Doctor *doctor) :Command(doctor){}
virtual void treat()
{
m_doctor->treatEye();
}
};
class CommandTreat_Nose :public Command
{
public:
CommandTreat_Nose(Doctor *doctor) :Command(doctor){}
virtual void treat()
{
m_doctor->treatNose();
}
};
//护士只能一个一个处理病例
class Nurse
{
public:
Nurse(Command*command)
{
m_command = command;
}
void submittedCaseCommand()
{
m_command->treat();
}
private:
Command*m_command;
};
//护士长可以批量处理病例
class AdvNurse
{
public:
AdvNurse()
{
m_list.clear();
}
void setCommand(Command*command)
{
m_list.push_back(command);
}
void summittedCaseCommand()
{
for (list<Command*>::iterator it = m_list.begin(); it != m_list.end(); it++)
{
(*it)->treat();
}
}
private:
list<Command*>m_list;
};
//护士值班
void main01()
{
Doctor *doctor = NULL;
Nurse *nurse = NULL;
Command *command1 = NULL;
Command *command2 = NULL;
doctor = new Doctor;
command1 = new CommandTreat_Eye(doctor);
command2 = new CommandTreat_Nose(doctor);
nurse = new Nurse(command1);
nurse->submittedCaseCommand();
delete nurse;
delete command1;
nurse = new Nurse(command2);
nurse->submittedCaseCommand();
delete nurse;
delete command2;
delete doctor;
}
//护士长值班
void main02()
{
Doctor *doctor = NULL;
AdvNurse *advnurse = NULL;
Command *command1 = NULL;
Command *command2 = NULL;
doctor = new Doctor;
command1 = new CommandTreat_Eye(doctor);
command2 = new CommandTreat_Nose(doctor);
advnurse = new AdvNurse;
advnurse->setCommand(command1);
advnurse->setCommand(command2);
advnurse->summittedCaseCommand();
delete advnurse;
delete command1;
delete command2;
delete doctor;
}
void main()
{
main02();
}
行为型模式之命令模式
最新推荐文章于 2024-01-23 00:00:00 发布