大话设计模式 商场促销 工厂模式和策略模式 c++

对与简单工厂和策略模式的理解:

策略模式是一种定义一系列算法的方法

策略模式就是用来封装算法的

区别在于主函数的 策略进行了进一步的封装 客户端不需要知道 从实例化到具体方法的过程 策略模式传入Context是个策略 在context进行具体的实现方法

工厂模式返回了一个方法

以下是工厂设计模式:

#include
using namespace std;

class Cash
{
protected:
    double money;
public:
Cash()
{
    money = 0;
}
void setCash(double m)
{
    money = m;
}
virtual double acceptCash() = 0;
};
class CashNormal:public Cash
{
public:
    double acceptCash()
    {
       return money;
    }
};
class CashRebate:public Cash
{
private:
    double rebate;
public:
    CashRebate(double r)
    {
        rebate = r;
    }
    double acceptCash()
    {
        return money * rebate;
    }
};
class CashReturn : public Cash
{
private:
    double Moneyreturn;
    double Moneycondition;
public:
    CashReturn(double mc,double mr)
    {
        Moneyreturn = mr;
        Moneycondition = mc;
    }
    double acceptCash()
    {
          if(money >= Moneycondition)
            return money - Moneyreturn;
          else
            return money;
    }
};
class CashFactory
{
private:
    Cash * cash;
public:
    Cash & createCashAccept(int type)
    {
        switch(type)
        {
        case 1://打八折
            cash = new CashRebate(0.8);break;
        case 2://满300减100
            cash = new CashReturn(300,100);break;
        case 3://正常收费
            cash = new CashNormal();break;
        }
        return *cash;
    }
};
int main()
{
    Cash *cash;
    CashFactory cfactory;
    int type;
    double money;
    cin >> type >> money;
    cash = &cfactory.createCashAccept(type);
    cash->setCash(money);
    cout << cash->acceptCash() << endl;;
    return 0;
}

以下是策略模式的概念:

#include
using namespace std;

class Strategy
{
public:
    virtual void AlgorithmInterface(){}
};
class ConcreteStrategyA:public Strategy
{
public:
    void AlgorithmInterface()
    {
        cout << "算法A"<< endl;
    }
};
class ConcreteStrategyB:public Strategy
{
public:
    void AlgorithmInterface()
    {
        cout << "算法B"<< endl;
    }
};
class ConcreteStrategyC:public Strategy
{
public:
    void AlgorithmInterface()
    {
        cout << "算法C"<< endl;
    }
};
class Context
{
private:
    Strategy *strategy;
public:
    Context(Strategy *a)
    {
        strategy = a;
    }
    void ContectInterface()
    {
        strategy->AlgorithmInterface();
    }
};
int main()
{
    Context *context;
    context = new Context(new ConcreteStrategyA());
    context->ContectInterface();

    context = new Context(new ConcreteStrategyB());
    context->ContectInterface();

    context = new Context(new ConcreteStrategyC());
    context->ContectInterface();

}
策略模式:

#include
using namespace std;

class Cash
{
public:
virtual double acceptCash(double money) = 0;
};
class CashNormal:public Cash
{
public:
    double acceptCash(double money)
    {
       return money;
    }
};
class CashRebate:public Cash
{
private:
    double rebate;
public:
    CashRebate(double r)
    {
        rebate = r;
    }
    double acceptCash(double money)
    {
        return money * rebate;
    }
};
class CashReturn : public Cash
{
private:
    double Moneyreturn;
    double Moneycondition;
public:
    CashReturn(double mc,double mr)
    {
        Moneyreturn = mr;
        Moneycondition = mc;
    }
    double acceptCash(double money)
    {
          if(money >= Moneycondition)
            return money - Moneyreturn;
          else
            return money;
    }
};
class CashContext //把所有策略都封装在一起
{
private:
    Cash *cs;
public:
    CashContext(Cash *csuper)
    {
        cs = csuper;
    }
    double GetResule(double money)
    {
        return cs->acceptCash(money);
    }
};
int main()
{
    CashContext *cc = NULL;
    int type;
    double money;
    cin >> type >> money;
    switch(type)
        {
        case 1://打八折
            cc = new CashContext(new CashRebate(0.8));break;
        case 2://满300减100
            cc =new CashContext(new CashReturn(300,100));break;
        case 3://正常收费
            cc = new CashContext(new CashNormal());break;
        }
       cout << cc->GetResule(money);
         return 0;
    }
策略与简单工厂相结合
#include
using namespace std;

class Cash
{
public:
virtual double acceptCash(double money) = 0;
};
class CashNormal:public Cash
{
public:
    double acceptCash(double money)
    {
       return money;
    }
};
class CashRebate:public Cash
{
private:
    double rebate;
public:
    CashRebate(double r)
    {
        rebate = r;
    }
    double acceptCash(double money)
    {
        return money * rebate;
    }
};
class CashReturn : public Cash
{
private:
    double Moneyreturn;
    double Moneycondition;
public:
    CashReturn(double mc,double mr)
    {
        Moneyreturn = mr;
        Moneycondition = mc;
    }
    double acceptCash(double money)
    {
          if(money >= Moneycondition)
            return money - Moneyreturn;
          else
            return money;
    }
};
class CashContext //把所有策略都封装在一起
{
private:
    Cash *cs;
public:
    CashContext(int type)
    {
        switch(type)
        {
        case 1:{
            CashNormal *cs0 = new CashNormal();
            cs = cs0;
            break;
        }

        case 2:
            {
            CashReturn *cs1 = new CashReturn(300,100);
            cs =cs1;
            break;
            }

        case 3:
            {
            CashRebate *cs2 = new CashRebate(0.8);
            cs = cs2;
            break;
            }

        }
    }
    double GetResule(double money)
    {
        return cs->acceptCash(money);
    }
};
int main()
{

    int type;
    double money;
    cin >> type >> money;
    CashContext cc(type);
    cout << cc.GetResule(money);
         return 0;
    }


你可能感兴趣的:(大话设计模式)