商场收银软件的简单工厂实现



 

#include<iostream>

using namespace std;

class CashSuper
{
public:
    virtual double acceptCash(double money) = 0;
};

class CashNormal : public CashSuper
{
public:
    double acceptCash(double money)
    {
        return money;
    }
};

class CashRebate : public CashSuper
{
private:
    double moneyRebate;

public:
    CashRebate(double moneyRebate_t) : moneyRebate(moneyRebate_t) {}

    double acceptCash(double money)
    {
        return money * moneyRebate;
    }
};

class CashReturn : public CashSuper
{
private:
    double moneyCondition;
    double moneyReturn;

public:
    CashReturn(double moneyCondition_t, double moneyReturn_t) : moneyCondition(moneyCondition_t), moneyReturn(moneyReturn_t) {}

    double acceptCash(double money)
    {
        double result = money;
        if (money >= moneyCondition)
            result = money = (money - moneyCondition)*moneyReturn;

        return result;
    }
};

class CashFactory
{
public:
    static CashSuper* createCashAccept(int type)
    {
        CashSuper *cs = nullptr;
        switch (type)
        {
        case 1:
            cs = new CashNormal;
            break;
        case 2:
            cs = new CashRebate(0.8);
            break;
        case 3:
            cs = new CashReturn(300, 100);
            break;
        }

        return cs;
    }
};

int main()
{
    double price, num;
    cout << "请输入单价和数量:" << endl;
    cin >> price >> num;
    double total = price * num;
    cout << "1.正常收费" << endl;
    cout<<"2.打8折"<<endl;
    cout<<"3.满300返100"<<endl;
    cout << "请输入促销条件前的数字序号: " << endl;
    int type;
    cin >> type;
    CashSuper *cs = CashFactory::createCashAccept(type);
    double result = cs->acceptCash(total);
    cout << result << endl;


    system("pause");
    return 0;
}


商场收银软件的策略模式加简单工厂实现



 

#include<iostream>

using namespace std;

/*定义所有支持的算法的公共接口*/
class CashSuper
{
public:
    virtual double acceptCash(double money) = 0;
};

/*封装了具体的算法或行为*/
class CashNormal : public CashSuper
{
public:
    /*具体算法的实现*/
    double acceptCash(double money)
    {
        return money;
    }
};

class CashRebate : public CashSuper
{
private:
    double moneyRebate;

public:
    CashRebate(double moneyRebate_t) : moneyRebate(moneyRebate_t) {}

    double acceptCash(double money)
    {
        return money * moneyRebate;
    }
};

class CashReturn : public CashSuper
{
private:
    double moneyCondition;
    double moneyReturn;

public:
    CashReturn(double moneyCondition_t, double moneyReturn_t) : moneyCondition(moneyCondition_t), moneyReturn(moneyReturn_t) {}

    double acceptCash(double money)
    {
        double result = money;
        if (money >= moneyCondition)
            result = money = (money - moneyCondition)*moneyReturn;

        return result;
    }
};

/*用一个ConcreteStrategy来配置,维护一个对Strategy对象的引用*/
class CashContext
{
private:
    CashSuper *cs = nullptr;
    int type;

public:
    /*集合了简单工厂模式,初始化时,传入具体的策略对象*/
    CashContext(int t): type(t)
    {
        switch(type)
        {
        case 1:
            cs = new CashNormal;
            break;
        case 2:
            cs = new CashRebate(0.8);
            break;
        case 3:
            cs = new CashReturn(300, 100);
            break;
        }
    }

    /*上下文接口,根据具体的策略对象,调用其算法的方法*/
    double GetResult(double money)
    {
        return cs->acceptCash(money);
    }

};

int main()
{
    double price, num;
    cout << "请输入单价和数量:" << endl;
    cin >> price >> num;
    double total = price * num;
    cout << "1.正常收费" << endl;
    cout<<"2.打8折"<<endl;
    cout<<"3.满300返100"<<endl;
    cout << "请输入促销条件前的数字序号: " << endl;
    int type;
    cin >> type;
    CashContext *csuper = new CashContext(type);
    double result = csuper->GetResult(total);
    cout << result << endl;


    system("pause");
    return 0;
}

策略模式解析

12-24 08:41