您的位置:首页 > 编程语言 > C语言/C++

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

2016-03-16 11:00 453 查看
简单记录一下策略模式,策略模式是一种定义一系列算法的方法,这些算法完成相同工作, 只是实现的细节不一样。那么我们可以利用策略模式, 封装算法, 实现算法与算法使用者之间的解耦工作

另外, 策略模式利于单元检测, 因为各个算法之间是相互独立的。

下面上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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: