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

本文对比分析了策略模式与简单工厂模式的区别与联系,并通过C++代码示例详细介绍了这两种设计模式的应用。策略模式用于封装算法,允许在运行时选择算法;而简单工厂模式则提供了一种创建对象的方式,避免了直接使用new关键字。最后还展示了策略模式与简单工厂模式结合使用的案例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

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

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

工厂模式返回了一个方法

以下是工厂设计模式:

#include<iostream>
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<iostream>
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<iostream>
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<iostream>
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;
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值