初学设计模式之命令模式
命令模式的代码例子
1 #include<iostream> 2 using namespace std; 3 class team1 4 { 5 public: 6 void DoSomeing() 7 { 8 cout<<"做一些事情"<<endl; 9 } 10 }; 11 12 class team2 13 { 14 public: 15 void DoSomeing() 16 { 17 cout<<"做一些事情"<<endl; 18 } 19 }; 20 21 class team3 22 { 23 public: 24 void DoSomeing() 25 { 26 cout<<"做一些事情"<<endl; 27 } 28 }; 29 30 class Command 31 { 32 protected: 33 team1 m_team1; 34 team2 m_team2; 35 team3 m_team3; 36 public: 37 virtual void exec() 38 {}; 39 40 }; 41 42 class ConcreteCommand1:public Command 43 { 44 public: 45 void exec() 46 { 47 m_team1.DoSomeing(); 48 }; 49 50 }; 51 class Invoker 52 { 53 private: 54 Command* m_CommandPtr; 55 public: 56 void AddCommand(Command *m_commandPtr) 57 { 58 m_CommandPtr=m_commandPtr; 59 }; 60 void Action() 61 { 62 m_CommandPtr->exec(); 63 }; 64 65 }; 66 67 68 int main() 69 { 70 ConcreteCommand1 m_ConcreteCommand1; 71 Invoker m_Invoker; 72 m_Invoker.AddCommand(&m_ConcreteCommand1); 73 m_Invoker.Action(); 74 getchar(); 75 return 0; 76 77 }