策略模式加工厂模式实现商场促销 C++
2016-03-16 11:00
453 查看
简单记录一下策略模式,策略模式是一种定义一系列算法的方法,这些算法完成相同工作, 只是实现的细节不一样。那么我们可以利用策略模式, 封装算法, 实现算法与算法使用者之间的解耦工作。
另外, 策略模式利于单元检测, 因为各个算法之间是相互独立的。
下面上UML图
这个图和简单工厂模式的图很类似, 不过简单工厂模式中 工厂类 和 产品基类之间是一种依赖关系, 而这里是一种聚合关系。
代码如下:
Csuper.h
MainSuper.h
strategyContext.h
main.cpp
另外, 策略模式利于单元检测, 因为各个算法之间是相互独立的。
下面上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; }
相关文章推荐
- C++的命名空间
- C++ inline 函数
- C++一元运算符的构造函数重载和友元函数重载
- 用C语言编写生成小学四则运算程序
- C++设计模式编程之Flyweight享元模式结构详解
- 动态规划C++::杭电ACM1003
- 运算符重载函数作为类成员函数与友元函数的区别
- C/C++ const 关键字详解
- 详解设计模式中的中介者模式在C++编程中的运用
- 关于C/C++获取指针大小判断系统位数的猜想
- 四道经典C语言指针试题
- c++二叉树的建立、前序中序后序深搜、宽搜、宽搜带行号
- c++中endl与“\n”的区别
- C++继承方式
- C++多态的实现及原理详细解析
- 重温C语言之--文件操作
- C++ 溢出与越界
- c实现各种进制间转换
- C语言+嵌入式SQL+DB2开发经验总结
- C++设计模式——单例模式 系列