UML图如下:

如果单使用策略模式,会出现这个问题:

客户端判断使用哪个算法!

这里可以用简单工厂与策略模式结合!

运行截图如下:

源码如下:

cash.h

#ifndef CASH_H
#define CASH_H#include <QString>class CashSuper{public:virtual double acceptCash(double money) = 0;virtual ~CashSuper(){}
};class CashNormal: public CashSuper{public:double acceptCash(double money);~CashNormal();
};class CashRebate: public CashSuper{public:CashRebate(const QString moneyRebate);double acceptCash(double money);~CashRebate();private:double m_moneyRebate;
};class CashReturn: public CashSuper{public:CashReturn(const QString moneyCondition, const QString moneyReturn);double acceptCash(double money);~CashReturn();private:double m_moneyCondition;double m_moneyReturn;
};class CashContext{public:CashContext(CashSuper *csuper);~CashContext();double getResult(double money);private:CashSuper *m_cs;
};#endif // CASH_H

widget.h

#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>namespace Ui {
class Widget;
}class Widget : public QWidget
{Q_OBJECTpublic:explicit Widget(QWidget *parent = 0);~Widget();protected:void insertListWidgetItem();void getCountPrice(double &countPrice);protected slots:void submitBtnClicked();void clearBtnClicked();private:Ui::Widget *ui;
};#endif // WIDGET_H

cash.cpp

#include "cash.h"
#include <QDebug>double CashNormal::acceptCash(double money)
{return money;
}CashNormal::~CashNormal()
{qDebug()<< "CashNormal::~CashSuper() called!";
}CashRebate::CashRebate(const QString moneyRebate)
{m_moneyRebate = moneyRebate.toDouble();
}double CashRebate::acceptCash(double money)
{return money * m_moneyRebate;
}CashRebate::~CashRebate()
{qDebug()<< "CashRebate::~CashRebate() called!";
}CashReturn::CashReturn(const QString moneyCondition, const QString moneyReturn)
{m_moneyCondition = moneyCondition.toDouble();m_moneyReturn = moneyReturn.toDouble();
}double CashReturn::acceptCash(double money)
{double result = money;if(money >= m_moneyCondition)result = money - (int)(money / m_moneyCondition) * m_moneyReturn;return result;
}CashReturn::~CashReturn()
{qDebug()<< "CashReturn::~CashReturn() called!";
}CashContext::CashContext(CashSuper *csuper)
{m_cs = csuper;
}CashContext::~CashContext()
{delete m_cs;
}double CashContext::getResult(double money)
{return m_cs->acceptCash(money);
}

main.cpp

#include "widget.h"
#include <QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);Widget w;w.show();return a.exec();
}

widget.cpp

#include "widget.h"
#include "ui_widget.h"
#include "cash.h"
#include <QMessageBox>
#include <QDebug>Widget::Widget(QWidget *parent) :QWidget(parent),ui(new Ui::Widget)
{ui->setupUi(this);this->setWindowTitle("CSDN IT1995");ui->rebateComboBox->addItem("正常收费");ui->rebateComboBox->addItem("满300减100");ui->rebateComboBox->addItem("打8折");ui->rebateComboBox->addItem("打5折");ui->rebateComboBox->addItem("抛出异常");connect(ui->submitPushButton, SIGNAL(clicked(bool)), this, SLOT(submitBtnClicked()));connect(ui->clearPushButton, SIGNAL(clicked(bool)), this, SLOT(clearBtnClicked()));
}Widget::~Widget()
{delete ui;
}void Widget::insertListWidgetItem()
{if(ui->goodsNumLineEdit->text().isEmpty() || ui->goodsPriceLineEdit->text().isEmpty())throw "goodsNumLineEdit or goodsPriceLineEdit is empty!";QString goodsNum = ui->goodsNumLineEdit->text();QString goodsPrice = ui->goodsPriceLineEdit->text();ui->goodsNumLineEdit->clear();ui->goodsPriceLineEdit->clear();ui->listWidget->addItem("商品单价:" + goodsPrice+ " 商品数量:" + goodsNum+ " 商品总价:" + QString::number(goodsPrice.toDouble() * goodsNum.toDouble()));
}void Widget::getCountPrice(double &countPrice)
{for(int i = 0; i < ui->listWidget->count(); i++){QStringList list = ui->listWidget->item(i)->text().split("商品总价:");countPrice += list[list.size() - 1].toDouble();}
}void Widget::submitBtnClicked()
{CashContext *cc = NULL;try{if(ui->rebateComboBox->currentText() == "正常收费"){cc = new CashContext(new CashNormal);}else if(ui->rebateComboBox->currentText() == "满300减100"){cc = new CashContext(new CashReturn("300", "100"));}else if(ui->rebateComboBox->currentText() == "打8折"){cc = new CashContext(new CashRebate("0.8"));}else if(ui->rebateComboBox->currentText() == "打5折"){cc = new CashContext(new CashRebate("0.5"));}else{throw "the text of rebateComboBox is unnormal!";}insertListWidgetItem();double countPrice = 0.0;getCountPrice(countPrice);QString priceStr = QString::number(cc->getResult(countPrice));ui->countPriceLabel->setText("总价:" + priceStr);}catch(const char *err){QMessageBox::information(this, "info", QString(err));}if(cc != NULL)delete cc;
}void Widget::clearBtnClicked()
{ui->listWidget->clear();ui->countPriceLabel->setText("总价:");
}

C++设计模式-使用Qt框架模拟策略模式(Strategy)商场促销相关推荐

  1. C++设计模式-使用Qt框架模拟策略模式(Strategy)+简单工厂实现商场促销

    商城促销: 1.简单工厂模式:客户端认识两个类,CashSuper与CashFactory 2.简单工厂模式 + 策略模式:客户端只要认识CashContext就可以了,更加降低耦合性 策略模式解析: ...

  2. 设计模式随笔系列:鸭子-策略模式(Strategy)[原]

    原文地址为: 设计模式随笔系列:鸭子-策略模式(Strategy)[原] 鸭子-策略模式(Strategy) 前言 万事开头难,最近对这句话体会深刻!这篇文章是这个系列正式开始介绍设计模式的第一篇,所 ...

  3. 设计模式(一):“穿越火线”中的“策略模式”(Strategy Pattern)

    在前段时间呢陆陆续续的更新了一系列关于重构的文章.在重构我们既有的代码时,往往会用到设计模式.在之前重构系列的博客中,我们在重构时用到了"工厂模式"."策略模式" ...

  4. 设计模式 ( 十八 ) 策略模式Strategy(对象行为型)

    设计模式 ( 十八 ) 策略模式Strategy(对象行为型) 1.概述 在软件开发中也经常遇到类似的情况,实现某一个功能有多种算法或者策略,我们能够依据环境或者条件的不同选择不同的算法或者策略来完毕 ...

  5. Java设计模式(十二) 策略模式

    策略模式介绍 策略模式定义 策略模式(Strategy Pattern),将各种算法封装到具体的类中,作为一个抽象策略类的子类,使得它们可以互换.客户端可以自行决定使用哪种算法. 策略模式类图 策略模 ...

  6. Java设计模式之行为型:策略模式

    一.背景: 在开发中经常遇到这种情况,实现某个功能有多种算法策略,我们可以根据不同环境或者条件选择不同的算法策略来完成该功能,比如查找.排序等,一种常用方式是硬编码在一个类中,如需要提供多种查找算法, ...

  7. 解读设计模式----策略模式(Strategy Pattern)

    一.模式概述      策略模式(Strategy Pattern)在外形上与状态模式很相似,但在意图上有些不同.其意图是使这些算法可以相互替换,并提供一种方法来选择最合适的算法.       在我应 ...

  8. 设计模式--策略模式(strategy)

    1.策略模式(strategy ['strætədʒi]) 我的理解是:方案候选模式 (反正关键就是有很多的候选,哈哈) 看了很多例子,都是在说鸭子的,那个例子很好,在这里可以看 他们生产鸭子,我们就 ...

  9. 设计模式-策略模式(Strategy)-Java

    设计模式-策略模式(Strategy)-Java 目录 文章目录 1.前言 2.示例案例-电影票打折方案 3.策略模式概述 3.1.策略模式定义 3.2.策略模式结构 3.3.策略模式结构图中角色 3 ...

最新文章

  1. oracle 9i 安装及连接远程数据库
  2. 机器学习 | 强化学习,解决决策问题的行家(上)
  3. Linux-Iptables-Memcached实现内网转发连接
  4. git仓库的简单使用
  5. 696. Count Binary Substrings 计数二进制子串
  6. 大剑无锋之delete、drop、truncate【面试推荐】
  7. C语言笔记:格式化输入输出(fprintf、fscanf、sscanf...)
  8. 【原型设计】实用节:Axure RP9 的一些常用的快捷按键组合操作
  9. python——爬虫实现网页信息抓取
  10. ecipse theme
  11. ssh secure shell client 保存密码_著名的SSH协议
  12. HTML(超文本标记语言)-----WEB开发基础之二
  13. 图像的几何变换maketform imtransform imresize imcrop
  14. 苹果录屏没声音_不会真有人MacBook录屏没声音吧?别用了SoundFlower了!
  15. java spring mvc json转对象,SpringMVC中使用@RequestBody,@ResponseBody注解实现Java对象和XML/JSON数据自动转换(上)......
  16. Matlab绘制跳动的心
  17. python 集合的基本操作
  18. 深圳南山区学位申请特殊住房需要的材料有哪些
  19. React 全家桶(react脚手架 redux react-redux react-router-dom ui库 reactHook)含 自定义hook的方法及使用
  20. DM642开发板复位芯片TL7705A

热门文章

  1. 肝了一个月,我做了个免费的面试刷题网
  2. 修改weblogic(10.3)域的启动JDK
  3. 图表如何又酷又实用?这个功能,能让大屏做出专家级效果
  4. 如何看待蒂姆·库克在苹果的地位
  5. 纵观软件行业开发方法论的发展
  6. [图]美专家称人类可能永远无法飞出太阳系
  7. 基于Nokia S60的游戏开发之二
  8. Win32的虚拟内存分配函数
  9. 获取Windows 系统的内核变量
  10. python while循环if_python – 使用if语句时陷入while循环