// 策略模式——商城促销
// 策略模式就是定义了一系列的算法,算法完成相同的工作但是实现不同,创建一个ConText类调用不同算法,减少各算法见的耦合
// CashSuper是抽象策略(算法接口)
// 其他三个子类就是具体策略(具体算法)
// CashContext用一个具体策略来配置,维护一个对抽象策略的引用
// Dialog是客户端代码
// 可以理解为,CashContext是controller,dialog是view,CashSuper是model
// 与MVC模式很像
CashSuper(理解为model)
// cashsuper.hpp
class CashSuper
{
public:
CashSuper() {};
virtual double acceptCashAndReturnTotal(const double cash) = 0;
};
CashNormal
class CashNormal : public CashSuper
{
public:
CashNormal();
double acceptCashAndReturnTotal(const double cash);
};
// cashnormal.cpp
#include "cashnormal.h"
CashNormal::CashNormal()
{
}
double CashNormal::acceptCashAndReturnTotal(const double cash)
{
return cash;
}
CashRebate
class CashRebate : public CashSuper
{
private:
double cashRebate;
public:
CashRebate(const double cashRebate);
double acceptCashAndReturnTotal(const double cash);
};
// cashrebate.cpp
#include "cashrebate.h"
CashRebate::CashRebate(const double cashRebate): cashRebate(cashRebate) {}
double CashRebate::acceptCashAndReturnTotal(const double cash)
{
return cash * cashRebate;
}
CashReturn
class CashReturn : public CashSuper
{
private:
double moneyCondition;
double moneyReturn;
public:
CashReturn(const double moneyCondition, const double moneyReturn);
double acceptCashAndReturnTotal(const double cash);
};
// cashreturn.cpp
#include "cashreturn.h"
CashReturn::CashReturn(const double moneyCondition, const double moneyReturn):
moneyCondition(moneyCondition), moneyReturn(moneyReturn)
{
}
double CashReturn::acceptCashAndReturnTotal(const double cash)
{
if(cash > moneyCondition) {
return cash - moneyReturn * (int)(cash / moneyCondition);
} else {
return cash;
}
}
CashContext(理解为控制器)
class CashContext
{
private:
CashSuper *cashSuper; // 申明一个抽象策略的对象(model)
QString way;
public:
CashContext(const QString way);
double getResult(const double cash);
};
//cashcontext.cpp
#include "cashcontext.h"
CashContext::CashContext(const QString way): way(way)
{ // 把判断移到控制器中,减少客户端对象的责任
// 简单工厂的应用
// 根据下拉框,选择不同的model
if (way == "正常收费") {
cashSuper = new CashNormal;
} else if (way == "打八折") {
cashSuper = new CashRebate(0.8);
} else if (way == "满300减100") {
cashSuper = new CashReturn(300, 100);
} else if (way == "打七折") {
cashSuper = new CashRebate(0.7);
}
}
double CashContext::getResult(const double cash)
{
return cashSuper -> acceptCashAndReturnTotal(cash);
}
客户端Dialog
class Dialog : public QDialog
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
private slots:
void on_pushButton_clicked();
private:
Ui::Dialog *ui;
double price;
int number;
double totalPrice;
QString text;
CashContext *cashAccept; // 申明一个控制器对象
};
// dialog.cpp
#include "dialog.h"
#include "ui_dialog.h"
#include <QDebug>
Dialog::Dialog(QWidget *parent) :
QDialog(parent),totalPrice(0),text(""),
ui(new Ui::Dialog)
{
ui->setupUi(this);
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::on_pushButton_clicked()
{
price = ui -> unit_price -> text().toDouble();
number = ui -> number -> text().toInt();
qDebug() << price;
QString temp;
// 根据下拉框选项,将对应的算法方式传入控制器对象中
cashAccept = new CashContext(ui -> comboBox -> currentText());
double result = cashAccept -> getResult(price * number);
text += temp.sprintf("%g", result);
text += "\n";
totalPrice += result;
ui -> textBrowser -> setText(text);
ui -> total -> setText(temp.sprintf("%g", totalPrice));
}