Command(命令)

单淳
2023-12-01

作用:
将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化;对请求排队或记录请求日志,以及支持可撤消的操作.

 

class Command
{
public:
virtual ~Command() {}
virtual void Execute() = 0;
};
class Receiver
{
public:
void Action();
};
class Invoker
{
public:
Invoker(Command *pCommand);
~Invoker();

class Command
{
public:
virtual ~Command() {}
virtual void Execute() = 0;
};
class Receiver
{
public:
void Action();
};
class Invoker
{
public:
Invoker(Command *pCommand);
~Invoker();

void Receiver::Action()
{
std::cout << "Receiver Action/n";
}
Invoker::Invoker(Command *pCommand)
: m_pCommand(pCommand)

{
}
Invoker::~Invoker()
{
delete m_pCommand;
m_pCommand = NULL;
}
void Invoker::Invoke()
{
if (NULL != m_pCommand)
{
m_pCommand->Execute();
}
}

ConcreateComand::ConcreateComand(Receiver* pReceiver)
: m_pReceiver(pReceiver)
{
}
ConcreateComand::~ConcreateComand()
{
delete m_pReceiver;
m_pReceiver = NULL;
}
void ConcreateComand::Execute()
{
if (NULL != m_pReceiver)
{
m_pReceiver->Action();
}
std::cout << "Execute by ConcreateComand/n";
}

int main()
{
Receiver* pReceiver = new Receiver();
Command* pCommand = new ConcreateComand(pReceiver);
Invoker* pInvoker = new Invoker(pCommand);
pInvoker->Invoke();
delete pInvoker;
system("pause");
return 0;
}

 

 

Involte存储继承Command的子类,通过Command命令的Execute操作来提交一个请求

 类似资料: