意图:
将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化,对请求排队或记录请求日志,以及支持可撤销的操作。
命令模式的构成:
1.客户角色:创建一个具体的命令对象,并确定其接收者
2.命令角色:声明一个给所有具体命令类的抽象接口,这是一个抽象角色,通常由一个接口或者抽象类实现
3.具体命令角色:定义一个接收者和行为之间的弱耦合,实现execute方法,负责调用接收者的相应操作。
4.请求者角色:负责调用命令对象执行请求
5.接收者角色:负责具体实施和执行一个请求。
具体实现代码;
*******************************************************************************************************
客户角色实现代码:main.CPP
*******************************************************************************************************
#include <QCoreApplication>
#include"reciever.h"
#include"command.h"
#include"concretecommand.h"
#include"invoke.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Reciever * rec = new Reciever;
command * comd = new concretecommand(*rec);
invoke * invk = new invoke;
invk->setcommand(*comd);
invk->excuteCommand();
return a.exec();
}***************************************************************************************************
命令角色实现代码:command.h
***************************************************************************************************
#ifndef COMMAND_H
#define COMMAND_H
#include"reciever.h"
class command
{
public:
command(Reciever & _reciever);
Reciever reciever;
virtual void excute()=0;
};
#endif // COMMAND_H************************************************************************************************
command.cpp
***********************************************************************************************
#include "command.h"
command::command(Reciever & _reciever)
{
reciever = _reciever;
}**********************************************************************************************
具体命令角色实现:concretecommand.h
*********************************************************************************************
#ifndef CONCRETECOMMAND_H
#define CONCRETECOMMAND_H
#include"command.h"
#include"reciever.h"
class concretecommand:public command
{
public:
concretecommand(Reciever & _reciever);
void excute();
};
#endif // CONCRETECOMMAND_H*******************************************************************************************
concretecommand.cpp
******************************************************************************************
#include "concretecommand.h"
concretecommand::concretecommand(Reciever & _reciever):command(reciever)
{
}
void concretecommand::excute()
{
reciever.Action();
}*****************************************************************************************
请求者实现代码:invoke.h
*****************************************************************************************
#ifndef INVOKE_H
#define INVOKE_H
#include"command.h"
class invoke
{
public:
invoke();
void setcommand( command & _comd);
void excuteCommand();
private:
command *comd;
};
#endif // INVOKE_H***********************************************************************************
invoke.cpp
***********************************************************************************
#include "invoke.h"
invoke::invoke()
{
}
void invoke::setcommand(command &_comd)
{
comd = &_comd;
}
void invoke::excuteCommand()
{
comd->excute();
}***********************************************************************************
接收者实现代码:reciever.h
*********************************************************************************
#ifndef RECIEVER_H
#define RECIEVER_H
class Reciever
{
public:
Reciever();
void Action();
};
#endif // RECIEVER_H
****************************************************************************
reciever.cpp
***************************************************************************
#include "reciever.h"
#include<QDebug>
Reciever::Reciever()
{
}
void Reciever::Action()
{
qDebug()<<"Reciever excuted";
}