首页 > 代码库 > 设计模式——命令模式(C++实现)

设计模式——命令模式(C++实现)

技术分享      

技术分享    

  1 [root@ ~/learn_code/design_pattern/19_order]$ cat order.cpp  2 #include <iostream>  3 #include <string>  4 #include <vector>  5 #include <algorithm>  6 #include <iterator>  7   8 using namespace std;  9  10 class Receiver 11 { 12 public: 13         void BakeMutton() 14         { 15                 cout<< "烤羊肉"<< endl; 16         } 17  18         void BakeChicken() 19         { 20                 cout<< "烤鸡翅"<< endl; 21         } 22 }; 23  24 class Command 25 { 26 public: 27         Command(Receiver* pstReceiver):m_pstReceiver(pstReceiver) 28         { 29  30         } 31         virtual void Excute() = 0; 32  33 protected: 34         Receiver* m_pstReceiver; 35 }; 36  37 class ConcreteCommandA: public Command 38 { 39 public: 40         ConcreteCommandA(Receiver* pstReceiver):Command(pstReceiver) 41         { 42  43         } 44         virtual void Excute() 45         { 46                 cout<< "ConcreteCommandA excuting......"<< endl; 47                 m_pstReceiver->BakeMutton(); 48         } 49  50 }; 51  52 class ConcreteCommandB: public Command 53 { 54 public: 55         ConcreteCommandB(Receiver* pstReceiver):Command(pstReceiver) 56         { 57  58         } 59         virtual void Excute() 60         { 61                 cout<< "ConcreteCommandB excuting......"<< endl; 62                 m_pstReceiver->BakeChicken(); 63         } 64 }; 65  66 class Invoke 67 { 68 public: 69         void Add(Command* pstCommand) 70         { 71                 m_vecPstCommand.push_back(pstCommand); 72         } 73         void Remove(Command* pstCommand) 74         { 75                 m_vecPstCommand.erase(find(m_vecPstCommand.begin(), m_vecPstCommand.end(), pstCommand)); 76         } 77         void RemoveAll() 78         { 79                 m_vecPstCommand.clear(); 80         } 81         void Notify() 82         { 83                 for (typeof(m_vecPstCommand.begin()) it = m_vecPstCommand.begin(); it != m_vecPstCommand.end(); ++it) 84                 { 85                         (*it)->Excute(); 86                 } 87         } 88  89 private: 90         vector<Command*> m_vecPstCommand; 91 }; 92  93 int main(int argc, char* argv[]) 94 { 95         Receiver* pstReceiver = new Receiver(); 96         Command* pstConcreteCommandA = new ConcreteCommandA(pstReceiver); 97         Command* pstConcreteCommandB = new ConcreteCommandB(pstReceiver); 98         Invoke* pstInvoke = new Invoke(); 99 100         pstInvoke->Add(pstConcreteCommandA);101         pstInvoke->Add(pstConcreteCommandA);102         pstInvoke->Add(pstConcreteCommandB);103         pstInvoke->Notify();104         cout<< "------------------"<< endl<< endl;105 106         pstInvoke->Remove(pstConcreteCommandA);  //撤销操作107         pstInvoke->Remove(pstConcreteCommandB);108         pstInvoke->Notify();109         cout<< "------------------"<< endl<< endl;110 111         return 0;112 }113 ////////////////////////////////////////114 [root@ ~/learn_code/design_pattern/19_order]$ ./order115 ConcreteCommandA excuting......116 烤羊肉117 ConcreteCommandA excuting......118 烤羊肉119 ConcreteCommandB excuting......120 烤鸡翅121 ------------------122 123 ConcreteCommandA excuting......124 烤羊肉125 ------------------

 

设计模式——命令模式(C++实现)