//Comnand模式
class Command
{
public:
Command(){}
virtual ~Command() {}
virtual void Execute() = 0;
};
class Receiver
{
public:
Receiver(){}
~Receiver(){}
void Action(){printf("action!\n");}
};
class Invoker
{
public:
Invoker(Command *pCommand){m_pCommand=pCommand;}
~Invoker(){}
void Invoke(){if(m_pCommand){m_pCommand->Execute();}}
private:
Command *m_pCommand;
};
class ConcreateComand: public Command
{
public:
ConcreateComand(Receiver* pReceiver):m_pReceiver(pReceiver){}
~ConcreateComand(){}
void Execute(){if(m_pReceiver)m_pReceiver->Action();}
private:
Receiver* m_pReceiver;
};
//Comnand模式
int _tmain(int argc, _TCHAR* argv[])
{
Receiver* pReceiver = new Receiver;
Command* pCommand = new ConcreateComand(pReceiver);
Invoker* pInvoker = new Invoker(pCommand);
pInvoker->Invoke();
delete pInvoker;
delete pCommand;
delete pReceiver;
getchar();
return 0;
}
转载于:https://my.oschina.net/u/221120/blog/890769