标签:pause 参数化 oncreate 回调 creat bsp receiver 模式 create
1) 意图
将一个请求封装为一个对象,从而使得可以用不同的请求对客户进行参数化;可以对请求排队或记录日志,以及撤销的操作
2) 结构
其中
3) 适用性
4) 举例
1 #include <iostream> 2 #include <list> 3 class Receiver 4 { 5 public: 6 Receiver() {} 7 virtual ~Receiver() {} 8 void Action() 9 { 10 std::cout << "Receiver Action" << std::endl; 11 } 12 }; 13 class Command 14 { 15 public: 16 Command(){} 17 virtual ~Command() {} 18 virtual void Execute() = 0; 19 }; 20 class ConcreateCommand : public Command 21 { 22 public: 23 ConcreateCommand(Receiver* ptr) :m_pReceiver(ptr) {} 24 virtual ~ConcreateCommand() 25 { 26 if (m_pReceiver) delete m_pReceiver; 27 } 28 virtual void Execute() 29 { 30 if (m_pReceiver) 31 { 32 m_pReceiver->Action(); 33 } 34 } 35 private: 36 Receiver* m_pReceiver; 37 }; 38 class Invoker 39 { 40 public: 41 Invoker(Command* ptr) :m_pCommand(ptr) {} 42 virtual ~Invoker() 43 { 44 if (m_pCommand) delete m_pCommand; 45 } 46 void Invoke() 47 { 48 if (m_pCommand) 49 { 50 m_pCommand->Execute(); 51 } 52 } 53 private: 54 Command* m_pCommand; 55 }; 56 57 int main() 58 { 59 Receiver* ptr1 = new Receiver(); 60 Command* ptr2 = new ConcreateCommand(ptr1); 61 Invoker* ptr3 = new Invoker(ptr2); 62 ptr3->Invoke(); 63 delete ptr3; 64 system("pause"); 65 }
标签:pause 参数化 oncreate 回调 creat bsp receiver 模式 create
原文地址:https://www.cnblogs.com/ho966/p/12233768.html