《大话设计模式》- 策略模式 - C++实现
2014-03-20 16:58
381 查看
common.h
strategy.h
context.h
main.cpp
/************************************************************************* > File Name: common.h > Author: banglang > Mail: banglang.huang@foxmail.com > Created Time: Thursday, March 20, 2014 PM01:57:44 CST ************************************************************************/ #ifndef _COMMON_H_ #define _COMMON_H_ #include<iostream> using namespace std; #endif /* _COMMON_H_ */
strategy.h
#ifndef _STRATEGY_H_ #define _STRATEGY_H_ #include "common.h" class cashSuper { public: virtual double acceptMoney(double) = 0; protected: double money; }; class cashNormal : public cashSuper { public: double acceptMoney(double money) { return money; } }; class cashReturn : public cashSuper { public: cashReturn(double _conditioin, double _ret) { this->cond = _conditioin; this->ret = _ret; } double acceptMoney(double money) { return ( money - ( ((int)(money / this->cond)) * this->ret) ); } private: double cond; double ret; }; class cashDiscount : public cashSuper { public: cashDiscount(double _rebate) { if(_rebate < 0) { this->rebate = 1; return; } this->rebate = _rebate; } double acceptMoney(double money) { return ( money * this->rebate ); } private: double rebate; }; #endif /* _STRATEGY_H */
context.h
#include "common.h" #include "strategy.h" #define ACPT_TYPE_NORMAL 0 #define ACPT_TYPE_RETURN 1 #define ACPT_TYPE_DISCOUNT 2 class CashContext { public: CashContext(int t) { this->cs = NULL; switch(t) { case ACPT_TYPE_NORMAL: cs = new cashNormal(); break; case ACPT_TYPE_RETURN: cs = new cashReturn(300, 10); break; case ACPT_TYPE_DISCOUNT: cs = new cashDiscount(0.8); break; default: cout<<"ACPT_TYPE Error, set as default:Normal\n"; cs = new cashNormal(); break; } } double acceptCash(double money) { if(!this->cs) { cout<"Object Error\n"; return 0; } return (this->cs->acceptMoney(money)); } private: cashSuper *cs; };
main.cpp
#include "common.h" #include "strategy.h" #include "context.h" int main(void) { CashContext *c1 = NULL; CashContext *c2 = NULL; CashContext *c3 = NULL; double money = 700; double result = 0; /*normal*/ c1 = new CashContext(ACPT_TYPE_NORMAL); result = c1->acceptCash(money); cout<<"result normal="<<result<<endl; /*discount*/ c2 = new CashContext(ACPT_TYPE_DISCOUNT); result = c2->acceptCash(money); cout<<"result discount="<<result<<endl; /*return*/ c3 = new CashContext(ACPT_TYPE_RETURN); result = c3->acceptCash(money); cout<<"result return="<<result<<endl; delete(c1); delete(c2); delete(c3); return 0; }
相关文章推荐
- 大话设计模式--策略模式 strategy -- C++实现实例
- 大话设计模式C++实现-第2章-策略模式
- 大话设计模式--策略模式 strategy -- C++实现实例
- 《大话设计模式》读书笔记(C++代码实现) 第二章:策略模式
- 读书 大话设计模式 第二章 策略模式 C++实现 1-2小节 未完
- 《大话设计模式》读书笔记(C++代码实现) 第二章:策略模式
- 大话设计模式——策略模式(C++实现)
- 大话设计模式C++实现--策略模式
- 大话设计模式 第二章 策略模式 C++实现
- 大话设计模式c++实现2——策略模式 Strategy Pattern
- 详解C++设计模式编程中策略模式的优缺点及实现
- 设计模式C++实现(2)——策略模式
- C++实现策略(Strategy)模式
- 大话设计模式C++实现-第14章-观察者模式
- 设计模式C++实现 —— 策略模式
- 大话设计模式c++实现--原型模式
- 设计模式---策略模式(C++实现)
- 策略模式 C++ 实现
- C++实现策略模式
- 我所理解的设计模式(C++实现)——策略模式(Strategy Pattern)