Command 模式

Command 模式,第1张

Command 模式

Command 模式通过将请求封装到一个对象(Command)中,并将请求的接受者存放具体的 ConcreteCommand 类中(Receiver)中,从而实现调用 *** 作的对象和 *** 作的具体实现

者之间的解耦。


Command 模式结构图中,将请求的接收者(处理者)放到 Command 的具体子类
ConcreteCommand 中,当请求到来时(Invoker 发出 Invoke 消息激活 Command 对象) ,
ConcreteCommand 将处理请求交给 Receiver对象进行处理。


 ////////////Command.h//////////////////////////////////////////////////////////////
#pragma once
class Reciever;
class Command
{
public:
virtual ~Command();
virtual void Excute() = ;
protected:
Command();
private:
}; class ConcreteCommand1 : public Command
{
public:
~ConcreteCommand1();
ConcreteCommand1(Reciever* rec);
void Excute() ;
protected:
private:
Reciever* _rec ;
}; class ConcreteCommand2 : public Command
{
public:
~ConcreteCommand2();
ConcreteCommand2(Reciever* rec);
void Excute() ;
protected:
private:
Reciever* _rec ;
};
 /////////Command.cpp/////////////////////////////////////////////////////////////////
#include "Command.h"
#include "Reciever.h"
Command::Command()
{ }
Command::~Command()
{ } ConcreteCommand1::ConcreteCommand1(Reciever* rec)
{
this->_rec = rec ;
}
void ConcreteCommand1::Excute()
{
this->_rec->Action1();
} ConcreteCommand1::~ConcreteCommand1()
{
delete _rec;
} ConcreteCommand2::ConcreteCommand2(Reciever* rec)
{
this->_rec = rec ;
}
void ConcreteCommand2::Excute()
{
this->_rec->Action2();
} ConcreteCommand2::~ConcreteCommand2()
{
delete _rec;
}
 //////////////Invoker.h////////////////////////////////////////////////////////////
#pragma once
#include <vector> class Command;
class Invoker
{
public:
void setcmd(Command* cmd);
void Notify();
protected:
private:
std::vector<Command*> _cmds ;
};
 //////Invoker.cpp////////////////////////////////////////////////////////////////////
#include "Invoker.h"
#include "Command.h"
#include <vector>
#include <iostream>
using namespace std;
void Invoker::setcmd(Command* cmd)
{
this->_cmds.push_back(cmd);
cout<<"增加请求"<<endl;
} void Invoker::Notify()
{
vector<Command*>::const_iterator it = _cmds.begin();
for (;it != _cmds.end() ; it++)
{
(*it)->Excute();
}
}
 ////////////Reciever.h//////////////////////////////////////////////////////////////
#pragma once
class Reciever
{
public:
~Reciever();
Reciever();
void Action1();
void Action2();
protected:
private:
};
 /////////Reciever.cpp/////////////////////////////////////////////////////////////////
#include "Reciever.h"
#include <iostream>
using namespace std ;
Reciever::Reciever()
{ }
Reciever::~Reciever()
{ }
void Reciever::Action1()
{
cout<<"处理具体的请求1"<<endl;
} void Reciever::Action2()
{
cout<<"处理具体的请求2"<<endl;
}
 ////////main.cpp//////////////////////////////////////////////////////////////////
#include "Command.h"
#include "Invoker.h"
#include "Reciever.h"
#include <iostream>
using namespace std;
int main()
{
Reciever* rec = new Reciever();//创建处理者
Invoker* inv = new Invoker();//创建请求者 Command* cmd1 = new ConcreteCommand1(rec);
Command* cmd2 = new ConcreteCommand2(rec); //得到请求
inv->setcmd(cmd1);
inv->setcmd(cmd2); //发出请求
inv->Notify(); getchar();
return ;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/zaji/588406.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-12
下一篇 2022-04-12

发表评论

登录后才能评论

评论列表(0条)

保存