博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

qt实现策略模式与工厂模式结合

Posted on 2011-01-05 11:20  ccmfc  阅读(1048)  评论(0编辑  收藏  举报

strategy.h头文件

#ifndef STRATEGY_H
#define STRATEGY_H

#include <qstring.h>
#include <ctype.h>
#include <string.h>

class CashSuper
{
public:
    virtual double GetCashResult(const double &money)=0;
};

class CashNormal:public CashSuper
{
public:
    double GetCashResult(const double &money)
    {
        return money;
    }
};

class CashReturn:public CashSuper
{
public:
    CashReturn(const QString &returnmoney, const QString &returncondition)
    {
        this->m_returncondition=returncondition.toDouble();
        this->m_returnmoney=returnmoney.toDouble();
    }
    double GetCashResult(const double &money)
    {
        if(money>=this->m_returncondition)
            return money-this->m_returnmoney;
        return money;
    }

private:
    double m_returnmoney;
    double m_returncondition;
};

class CashDiscount:public CashSuper
{
public:
    CashDiscount(const QString &Discount)
    {
        this->m_Discount=Discount.toDouble();
    }
    double GetCashResult(const double &money)
    {
        return money*this->m_Discount*0.1;
    }
private:
    double m_Discount;
};


class CashFactory
{
public:
   CashSuper* CreateCash(const size_t &type, QString stype)  //生成对象
    {
        CashSuper* pObject;

        switch(type)
        {
        case 0:
            pObject=new CashNormal;
            break;
        case 1:
            pObject=new CashReturn(GetQStringInt(stype), GetQStringInt(stype));
            break;
        case 2:

            pObject=new CashDiscount(GetQStringInt(stype));
            break;
        }

        return pObject;
    }

private:
   QString GetQStringInt(QString &pSrc)    //找出数字用来计算
   {
       QString tmp;
       char* ptmp=(char*)malloc(pSrc.length()+1);
       int pos=0;
       int saveaddr=(int)ptmp;

       strcpy(ptmp,pSrc.toLatin1().data());
       while(!isdigit(*ptmp++));
       ptmp--;

       while(isdigit(*ptmp)|| '.'==*ptmp)
       {
           tmp.append(*ptmp);
           ptmp++;
       }
       pos=pSrc.indexOf(pSrc);
       pSrc=pSrc.right(pos+tmp.length());

       ptmp=(char*)saveaddr;
       free(ptmp);
       ptmp=NULL;
       return tmp;
   }
};

#endif // STRATEGY_H

//--------------------------------------------------
 
#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include "strategy.h"

namespace Ui {
    class Dialog;
}


class Dialog : public QDialog
{
    Q_OBJECT

public:
    explicit Dialog(QWidget *parent = 0);
    ~Dialog();

private slots:
    void on_comboBox_aritype_activated(QString );
    void on_pushButton_reset_clicked();
    void on_pushButton_sure_clicked();

private:
    Ui::Dialog *ui;
    CashSuper *m_pCashSuper;

};

#endif // DIALOG_H

//--------------------------------------------------

#include "dialog.h"

#include "ui_dialog.h"

Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);
    ui->comboBox_aritype->insertItem(0, tr("正常收费"));
    ui->comboBox_aritype->insertItem(1, tr("满100返20"));
    ui->comboBox_aritype->insertItem(2, tr("7.5折"));
    ui->comboBox_aritype->setCurrentIndex(0);

}

Dialog::~Dialog()
{
    delete ui;
}


void Dialog::on_pushButton_sure_clicked()
{
    CashFactory ft;
    double totalprice=0.0;

    m_pCashSuper = ft.CreateCash(ui->comboBox_aritype->currentIndex(), ui->comboBox_aritype->currentText());
    totalprice=ui->lineEdit_price->text().toDouble()*ui->lineEdit_number->text().toUInt();
    totalprice=m_pCashSuper->GetCashResult(totalprice);
    ui->label_totalprice->setText(QString::number(totalprice, 12, 2)+tr("元"));
    ui->textEdit_show->setText(tr("总价=")+ui->label_totalprice->text()+tr("\t类型:")+ui->comboBox_aritype->currentText());

}

void Dialog::on_pushButton_reset_clicked()
{
    ui->lineEdit_number->setText("");
    ui->lineEdit_price->setText("");
    ui->comboBox_aritype->setCurrentIndex(0);
    ui->textEdit_show->setText("");
    ui->label_totalprice->setText("");
}

void Dialog::on_comboBox_aritype_activated(QString )    //动态修改
{
    if(ui->comboBox_aritype->currentIndex()==0)
         ui->comboBox_aritype->setEditable(false);
     ui->comboBox_aritype->setEditable(true);
}

//--------------------------------------------------

#include <QtGui/QApplication>
#include "dialog.h"
#include <qtextcodec.h>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QTextCodec::setCodecForTr(QTextCodec::codecForName("gb2312"));
    Dialog w;
    w.show();

    return a.exec();
}


//策略模式是一种定义一系列算法的方法,从概念上来看,所有这些算法完成的相同的工作,只是实现不同,
//它可以以相同的方式调用所有的算法,减少了各种算法类与使用算法类之间的耦合,