1.意图
将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化;对请求排队或记录请求日志,以及支持可撤销的操作。
2.别名
动作(Action),事务(Transaction)
3.动机
命令模式通过将请求本身变成一个对象来使请求可向未指定的应用对象提出请求。这个对象可被存储并像其他的对象一样被传递。
4.适用性
- 抽象出待执行的动作以参数化某对象。
- 在不同的时刻指定、排列和执行请求
- 支持取消操作。
- 支持修改日志,这样当系统崩溃时,这些修改可以被重做一遍
- 用构建在原语操作上的高层操作构造一个系统。
5.结构
6.代码实例
//Command.h #include <memory> class AbstractReceiver; class AbstractCommand { public: virtual void Execute() = 0; virtual void SetReceiver(std::shared_ptr<AbstractReceiver> receiver ); protected: std::shared_ptr<AbstractReceiver> m_Receiver; }; class ConcreteCommand1 : public AbstractCommand { public: void Execute(); }; class ConcreteCommand2 : public AbstractCommand { public: void Execute(); };
//Receiver.h class AbstractReceiver { public: virtual void Action() = 0; }; class ConcreteReceiver1 : public AbstractReceiver { public: void Action(); }; class ConcreteReceiver2 : public AbstractReceiver { public: void Action(); };
//Command.cpp #include <iostream> #include "Receiver.h" #include "Command.h" void AbstractCommand::SetReceiver(std::shared_ptr<AbstractReceiver> receiver) { m_Receiver = receiver; } void ConcreteCommand1::Execute() { m_Receiver->Action(); }; void ConcreteCommand2::Execute() { m_Receiver->Action(); };
//Receiver.cpp #include <iostream> #include "Receiver.h" void ConcreteReceiver1::Action() { std::cout << "Receiver1 Action Executed" << std::endl; }; void ConcreteReceiver2::Action() { std::cout << "Receiver2 Action Executed" << std::endl; };
//Client.cpp #include "Command.h" #include "Receiver.h" int main() { std::shared_ptr<AbstractCommand> pConcreteCommand1(new ConcreteCommand1); std::shared_ptr<AbstractCommand> pConcreteCommand2(new ConcreteCommand2); std::shared_ptr<AbstractReceiver> pReceiver1(new ConcreteReceiver1); std::shared_ptr<AbstractReceiver> pReceiver2(new ConcreteReceiver2); pConcreteCommand1->SetReceiver(pReceiver1); pConcreteCommand2->SetReceiver(pReceiver2); pConcreteCommand1->Execute(); pConcreteCommand2->Execute(); while(1); }
7.测试结果
8.效果
- 将调用操作的对象与知道如何实现该操作的对象解耦。
- Command对象是头等的对象。它可以像其它对象一样被操作和扩展。
- 可以将多个命令装配成一个命令。
- 增加新的Command很容易,因为无需改变已有的类。
时间: 2024-11-05 14:49:45