标签:
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.效果
标签:
原文地址:http://www.cnblogs.com/mgp200866130/p/5618002.html