策略模式加工厂模式实现商场促销 C++

81 篇文章 0 订阅
29 篇文章 1 订阅

简单记录一下策略模式,策略模式是一种定义一系列算法的方法,这些算法完成相同工作, 只是实现的细节不一样。那么我们可以利用策略模式, 封装算法, 实现算法与算法使用者之间的解耦工作
另外, 策略模式利于单元检测, 因为各个算法之间是相互独立的。

下面上UML图
这里写图片描述
这个图和简单工厂模式的图很类似, 不过简单工厂模式中 工厂类 和 产品基类之间是一种依赖关系, 而这里是一种聚合关系。

代码如下:
Csuper.h

#ifndef _CASHSUPER_H_
#define _CASHSUPER_H_

template <class T>
class CCashSuper{
public:
    /************************************************************************/
    /* 收取现金的接口                                                       */
    /************************************************************************/
    virtual double acceptCash(T money) const /*{ return money; }*/ = 0;
};

#endif // _CASHSUPER_H_

MainSuper.h

#ifndef _MAINCASHSTRATEGY_H_
#define _MAINCASHSTRATEGY_H_

#include "CashSuper.h"

/************************************************************************/
/* 正常收费的策略                                                       */
/************************************************************************/
template<class T>
class CCashNormal : public CCashSuper<T>{
public:
    T acceptCash(T money) const{
        return money;
    }
};

/************************************************************************/
/* 打折收费的策略                                                       */
/************************************************************************/
template<class T, class U>
class CCashRebate : public CCashSuper<T>{
public:
    CCashRebate(T debate) : debate(debate){}
    // 操作 debate 数据
    void setRebate(U debate){ this->debate = debate; }
    U getRebate() const { return debate; }

    // 计算收费
    T acceptCash(T money) const{
        return money * getRebate();
    }

private:
    U debate;
};

/************************************************************************/
/* 返现收费的策略                                                       */
/************************************************************************/
template<class T, class U>
class CCashReturn : public CCashSuper<T>{
public:
    CCashReturn(T moneyCond, T moneyRet) : moneyCond(moneyCond), moneyRet(moneyRet){}
    // manipulate the moneyCond
    U getMoneyCond() const { return moneyCond; }
    void setMoneyCond(U val) { moneyCond = val; }

    // manipulate the moneyRet
    U getMoneyRet() const { return moneyRet; }
    void setMoneyRet(U val) { moneyRet = val; }

    // calc the money
    T acceptCash(T money) const{
        return money >= moneyCond ? money - moneyRet : money;
    }

private:
    U moneyCond;        //  返现标准    
    U moneyRet;         //  返现额度    
};

#endif // _MAINCASHSTRATEGY_H_

strategyContext.h

#ifndef _STRATEGYCONTEXT_H_
#define _STRATEGYCONTEXT_H_

#include "MainCashStrategy.h"
#include <memory>

template <class T>
class CStrategyContext{
public:
    CStrategyContext(int type){
        switch (type)
        {
            case 0: // normal cash
                strategy = std::shared_ptr<CCashSuper<T>>(new CCashNormal<T>());
                break;
            case 1: // rebate cash
                strategy = std::shared_ptr<CCashSuper<T>>(new CCashRebate<T, double>(0.8));
                break;
            case 2: // return cash
                strategy = std::shared_ptr<CCashSuper<T>>(new CCashReturn<T, double>(300.0, 100.0));
                break;

            default:
                break;
        }
    }

    T getResult(T money) const {
        return strategy->acceptCash(money);
    }

private:
    std::shared_ptr<CCashSuper<T>> strategy;
};

#endif // _STRATEGYCONTEXT_H_

main.cpp

#include <iostream>
#include <memory>
#include "StrategyContext.h"

using namespace std;

int main(){
    shared_ptr<CStrategyContext<double>> context(new CStrategyContext<double>(2));
    cout << context->getResult(300.0) << endl;

    system("pause");
    return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值