command模式
来源:互联网 发布:西安seo技术 编辑:程序博客网 时间:2024/06/05 22:50
//command 模式
class Command
{
public:
virtual ~Command(){}
virtual void Execute() = 0;
};
class Receiver
{
public:
void Action();
};
void Receiver::Action()
{
std::cout << "receiver action\n";
}
class ConcreteCommand : public Command
{
public:
ConcreteCommand(Receiver* pReceiver);
~ConcreteCommand();
virtual void Execute();
private:
Receiver* m_pReceiver;
};
ConcreteCommand::ConcreteCommand(Receiver* pReceiver)
:m_pReceiver(pReceiver)
{
}
ConcreteCommand::~ConcreteCommand()
{
SAFE_DELETE(m_pReceiver);
}
void ConcreteCommand::Execute()
{
if (NULL != m_pReceiver)
{
m_pReceiver->Action();
}
std::cout << "execute by concreteCommand\n";
}
class Invoke
{
public:
Invoke(Command* pCommand);
~Invoke();
void Call();
private:
Command* m_pCommand;
};
Invoke::Invoke(Command* pCommand)
:m_pCommand(pCommand)
{
}
Invoke::~Invoke()
{
SAFE_DELETE(m_pCommand);
}
void Invoke::Call()
{
if (NULL != m_pCommand)
{
m_pCommand->Execute();
}
}
int main()
{
/*
command模式
主要包括三个组件 receiver <---- command <--- invoker (指针调用 持有目标的指针)
command中保存有receiver的指针,这样command在execute的时候直接调用他的receiver执行就可以
invoker也保存有command的指针,这样 当我想要触发invoker的时候 invoker->call 就直接调用 它的command的execute就行了
这就是command模式
命令的出发和执行解耦合,我只需要执行触发,命令的执行会自动进行,因为命令的执行者包括在命令中 所以叫作命令模式
Receiver* r = new Receiver();
Command* c = new ConcreteCommand(r);
Invoke* i = new Invoke(c);
i->Call();
delete i;
*/
}
- Command模式
- Command模式
- Command模式
- Command 模式
- Command 模式
- Command模式
- command模式
- Command模式
- Command模式
- Command模式
- Command 模式
- Command 模式
- Command模式
- Command模式
- Command模式
- COMMAND 模式
- Command模式
- Command模式
- oracle listagg 用法
- 解决Warning: World-writable config file ‘/etc/my.cnf’ is ignored
- 多布局
- Android数据存储(三)---File存储
- 数塔(dp)
- command模式
- 基于百度AI的语音合成
- POJ—1019 Number Sequence
- Python网络爬虫与信息提取-Day4-网络爬虫及Robots协议
- c++中如何写参数不定的函数
- 使用CSDN-markdown编辑器编辑csdn博客
- Jenkins 的部署(Windows)
- EasyUI DataGrid 时间格式化、字符串长度截取
- 图论总结(5)2—SAT问题