c++/qt设计模式-命令模式

文章中部分内容和思路来自《Head First设计模式》

 

模式定义

将"请求"封装成对象,以便使用不同的请求、队列或日志来参数化其他对象。命令模式也支持可撤销的

 

模式类图

70

 

典型案例

1.案例说明

假设客户去银行办理存取款业务,业务员根据客户业务的不同对客户账户进行存取款操作。在这个案例中,我们可以将业务员看做命令的发出者,客户账号看做命令接收者,具体命令是存款取款

 

2.代码实现

----------

ICommond.h

----------

 

#ifndef ICOMMOND_H
#define ICOMMOND_H
 
class ICommond
{
public:
    virtual void execute() = 0;
};
 
#endif // ICOMMOND_H

 

----------------

MoneyInCommond.h

----------------

 

#ifndef MONEYINCOMMOND_H
#define MONEYINCOMMOND_H
 
#include "ICommond.h"
 
#include "Account.h"
 
class MoneyInCommond : public ICommond
{
public:
    MoneyInCommond(Account *acc, float amount);
    virtual ~MoneyInCommond();
 
public:
    virtual void execute();
 
private:
    Account *_acc;
    float _amount;
};
 
#endif // MONEYINCOMMOND_H
 

 

------------------

MoneyInCommond.cpp

------------------

 

#include "MoneyInCommond.h"
 
MoneyInCommond::MoneyInCommond(Account *acc, float amount)
{
    _acc = acc;
    _amount = amount;
}
 
MoneyInCommond::~MoneyInCommond()
{
 
}
 
void MoneyInCommond::execute()
{
    _acc->moneyIn(_amount);
}
 

 

-----------------

MoneyOutCommond.h

-----------------

 

#ifndef MONEYOUTCOMMOND_H
#define MONEYOUTCOMMOND_H
 
#include "ICommond.h"
#include "Account.h"
 
class MoneyOutCommond : public ICommond
{
public:
    MoneyOutCommond(Account *acc, float amount);
    virtual ~MoneyOutCommond();
 
public:
    virtual void execute();
 
private:
    Account *_acc;
    float _amount;
};
 
#endif // MONEYOUTCOMMOND_H
 

 

-------------------

MoneyOutCommond.cpp

-------------------

 

#include "MoneyOutCommond.h"
 
MoneyOutCommond::MoneyOutCommond(Account *acc, float amount)
{
    _acc = acc;
    _amount = amount;
}
 
MoneyOutCommond::~MoneyOutCommond()
{
 
}
 
void MoneyOutCommond::execute()
{
    _acc->moneyOut(_amount);
}
 

 

---------

Account.h

---------

 

#ifndef ACCOUNT_H
#define ACCOUNT_H
 
 
class Account
{
public:
    Account();
 
public:
    void moneyIn(float value); // 存钱
    void moneyOut(float value); // 取钱
    float getTotal() const; // 获取总额
 
private:
    float _total;
};
 
#endif // ACCOUNT_H
 

 

-----------

Account.cpp

-----------

 

#include "Account.h"
 
Account::Account()
{
    _total = 0;
}
 
void Account::moneyIn(float value)
{
    _total += value;
}
 
void Account::moneyOut(float value)
{
    _total -= value;
}
 
float Account::getTotal() const
{
    return _total;
}
 

 

---------

Invoker.h

---------

 

#ifndef INVOKER_H
#define INVOKER_H
 
#include "ICommond.h"
 
class Invoker
{
public:
    Invoker();
 
public:
    void setCommond(ICommond *cmd);
    void commondExecute();
 
private:
    ICommond *_cmd;
};
 
#endif // INVOKER_H
 

 

-----------

Invoker.cpp

-----------

 

#include "Invoker.h"
 
Invoker::Invoker()
{
 
}
 
void Invoker::setCommond(ICommond *cmd)
{
    _cmd = cmd;
}
 
void Invoker::commondExecute()
{
    _cmd->execute();
}
 

 

--------

main.cpp

--------

 

/**
 * 设计模式-命令模式
 * 要素:1,执行者 2,抽象命令 3,具体命令 4,接受者[作用对象]
 */
#include <QCoreApplication>
#include <QDebug>
 
#include "Account.h"
#include "Invoker.h"
#include "MoneyInCommond.h"
#include "MoneyOutCommond.h"
 
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
 
 
    Account *acc = new Account;
    MoneyInCommond *inCmd = new MoneyInCommond(acc, 34);
    Invoker *bankAssist = new Invoker;
 
    
    bankAssist->setCommond(inCmd); // 银行职员先给账户存钱
    bankAssist->commondExecute();
 
    
    qDebug() << "账户余额:" << acc->getTotal();
 
    
    MoneyOutCommond *outCmd = new MoneyOutCommond(acc, 45);
    bankAssist->setCommond(outCmd); // 银行职员从账户取钱
    bankAssist->commondExecute();
 
    
    qDebug() << "账户余额:" << acc->getTotal();
 
    
    delete outCmd;
    outCmd = NULL;
 
    
    delete bankAssist;
    bankAssist = NULL;
 
    
    delete inCmd;
    inCmd = NULL;
 
    
    delete acc;
    acc = NULL;
 
    
    return a.exec();
}
 

 

案例不太好,但基本能说明问题,《Head First》一书中举了一个遥控器的例子,有兴趣的可以看看

 

 

 

 

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
C++ Qt设计模式(第2版)是美国萨福克大学已使用十余年的经典教程,利用跨平台开源软件开发框架Qt阐释了C++设计模式中的主要思想。全书共分四个部分:第一部分介绍C++、UML、Qt、模型-视图、SQL、XML、设计模式等基础知识,目的是为零基础的C++初学者铺垫一条学习面向对象编程的快捷之路;第二部分讲解内存访问、继承等重要的C++特性,是前一部分的延伸和拓展;第三部分使用Phonon编写了一个多媒体播放器,展示了主要技术理念的应用方法;附录部分给出了C++保留关键字、Debian和Qt程序开发环境的配置等内容。每节的练习题和各章后面的复习题,既可作为课堂上的讨论题,也可进一步启发读者对于关键知识点的思考。 C++ Qt设计模式(第2版)目录 第一部分 设计模式Qt 第1章 C++简介 2 第2章 类与对象 46 第3章 Qt简介 78 第4章 列表 85 第5章 函数 94 第6章 继承与多态 116 第7章 库与设计模式 163 第8章 QObject, QApplication,信号和槽 179 第9章 窗件和设计师 195 第10章 主窗口和动作 225 第11章 范型和容器 246 第12章 元对象,属性和反射编程 262 第13章 模型和视图 277 第14章 验证和正则表达式 302 第15章 XML解析 318 第16章 更多的设计模式 335 第17章 并发 353 第18章 数据库编程 376 第二部分 C++语言规范 第19章 类型与表达式 386 第20章 作用域与存储类 416 第21章 内存访问 431 第22章 继承详解 443 第三部分 编 程 作 业 第23章 MP3自动点唱机作业 456

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SuperYang_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值