DesignPattern/15.CommandPattern/2.Code/main.cpp

55 lines
976 B
C++
Raw Normal View History

2019-10-30 14:54:41 +00:00
#include <iostream>
#include "CommandPattern.h"
int main()
{
2021-10-28 15:15:51 +00:00
// 实例化调用者:按钮
2019-10-30 14:54:41 +00:00
Button *button = new Button();
Command *lampCmd, *fanCmd;
2021-10-28 15:15:51 +00:00
// 按钮控制电灯
2019-10-30 14:54:41 +00:00
lampCmd = new LampCommand();
button->setCommand(lampCmd);
button->touch();
button->touch();
button->touch();
printf("\n\n");
2021-10-28 15:15:51 +00:00
// 按钮控制风扇
2019-10-30 14:54:41 +00:00
fanCmd = new FanCommand();
button->setCommand(fanCmd);
button->touch();
button->touch();
button->touch();
#ifdef COMMAND_QUEUE
printf("\n\n***********************************\n");
Button2 *button2 = new Button2();
Command *lampCmd2, *fanCmd2;
CommandQueue *cmdQueue = new CommandQueue();
lampCmd2 = new LampCommand();
cmdQueue->addCommand(lampCmd2);
fanCmd2 = new FanCommand();
cmdQueue->addCommand(fanCmd2);
button2->setCommandQueue(cmdQueue);
button2->touch();
#endif
printf("\n\n");
2020-11-29 03:17:40 +00:00
delete button;
delete lampCmd;
delete fanCmd;
delete button2;
delete lampCmd2;
delete fanCmd2;
2019-10-30 14:54:41 +00:00
system("pause");
return 0;
}