[img]https://api5.yunpan.360.cn/intf.php?method=Share.getPublicThumbByNid&qid=108635719&nid=13770720612900291&size=800_600&devtype=web&v=1.0.1&rtick=14676146428611&share_qid=108635719&sign=df4c1667350df926ee18fdacc755a17d&[/img]
[img]https://api5.yunpan.360.cn/intf.php?method=Share.getPublicThumbByNid&qid=108635719&nid=14665838309468885&size=800_600&devtype=web&v=1.0.1&rtick=14665838997753&share_qid=108635719&sign=935089d0615eedc8867f9bd4a8ab63cd&[/img]
[img]https://api5.yunpan.360.cn/intf.php?method=Share.getPublicThumbByNid&qid=108635719&nid=14665838329487458&size=800_600&devtype=web&v=1.0.1&rtick=14665838993852&share_qid=108635719&sign=0c12173f3f6846f4e6a1d5cbd984c0a3&[/img]
[color=red][b]头文件[/b][/color]
[color=red][b]实现[/b][/color]
[color=red][b]客户端[/b][/color]
[color=red][b]运行结果[/b][/color]
[img]https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1466571446&di=20ae1e1632a15ea7c7026e717607590a&src=http://images.cnblogs.com/cnblogs_com/JackyTecblog/201211/201211050903506952.png[/img]
[img]https://api5.yunpan.360.cn/intf.php?method=Share.getPublicThumbByNid&qid=108635719&nid=14665838309468885&size=800_600&devtype=web&v=1.0.1&rtick=14665838997753&share_qid=108635719&sign=935089d0615eedc8867f9bd4a8ab63cd&[/img]
[img]https://api5.yunpan.360.cn/intf.php?method=Share.getPublicThumbByNid&qid=108635719&nid=14665838329487458&size=800_600&devtype=web&v=1.0.1&rtick=14665838993852&share_qid=108635719&sign=0c12173f3f6846f4e6a1d5cbd984c0a3&[/img]
[color=red][b]头文件[/b][/color]
//CommandPattern.h
#ifndef COMMAND_PATTERN_H
#define COMMAND_PATTERN_H
#include <Windows.h>
#include <string>
using namespace std;
namespace CommandPattern
{
class Receiver
{
public:
void Action();
};
class AbstructCommand
{
public:
virtual ~AbstructCommand();
virtual void Execute() = 0;
};
class ConcretetCommand : public AbstructCommand
{
public:
ConcretetCommand(Receiver* pReciver);
virtual ~ConcretetCommand();
virtual void Execute();
private:
Receiver* m_pReciver;
};
class Invoker
{
public:
Invoker(AbstructCommand* pCommand);
void Order();
private:
AbstructCommand* m_pCommand;
};
//
void CommandPattern_Test();
}
#endif
[color=red][b]实现[/b][/color]
#include "CommandPattern.h"
#include <iostream>
using namespace std;
#define SAFE_DELETE(p) if (p) { delete p; p = NULL; }
namespace CommandPattern
{
//
void Receiver::Action()
{
cout << "Receiver->Action" << endl;
}
//
AbstructCommand::~AbstructCommand()
{
}
//
ConcretetCommand::ConcretetCommand(Receiver* pReciver)
: m_pReciver(NULL)
{
if (m_pReciver==NULL && pReciver!=NULL)
{
m_pReciver = pReciver;
}
}
ConcretetCommand::~ConcretetCommand()
{
}
void ConcretetCommand::Execute()
{
if (m_pReciver != NULL)
{
cout << "ConcretetCommand->Execute" << endl;
m_pReciver->Action();
}
}
//
Invoker::Invoker(AbstructCommand* pCommand)
: m_pCommand(NULL)
{
if (m_pCommand==NULL && pCommand!=NULL)
{
m_pCommand = pCommand;
}
}
void Invoker::Order()
{
cout << "Invoker->Order" << endl;
m_pCommand->Execute();
}
void CommandPattern_Test()
{
Receiver* pReceiver = new Receiver();
ConcretetCommand* pConcreteCmd = new ConcretetCommand(pReceiver);
Invoker* pInvoker= new Invoker(pConcreteCmd);
pInvoker->Order();
SAFE_DELETE(pReceiver);
SAFE_DELETE(pConcreteCmd);
SAFE_DELETE(pInvoker);
}
}
[color=red][b]客户端[/b][/color]
#include "CommandPattern.h"
#include <iostream>
using namespace std;
using namespace CommandPattern;
void main()
{
CommandPattern_Test();
}
[color=red][b]运行结果[/b][/color]
Invoker->Order
ConcretetCommand->Execute
Receiver->Action
[img]https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1466571446&di=20ae1e1632a15ea7c7026e717607590a&src=http://images.cnblogs.com/cnblogs_com/JackyTecblog/201211/201211050903506952.png[/img]