设计模式 行为方法模式(十命令模式)

意图:

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


命令模式的构成:

   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";
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值