策略模式(STRATEGY PATTERN )
来源:互联网 发布:交易策略评估与最优化 编辑:程序博客网 时间:2024/06/11 20:57
策略模式(STRATEGY PATTERN ) (2012-09-30 13:49)
标签: 策略 分类: DesignPattern
strtegy.h
点击(此处)折叠或打开
- #ifndef _STRTEGY_H_
- #define _STRTEGY_H_
- /**
- * @brief 锦囊妙计(抽象类)
- */
- class Strategy
- {
- public:
- virtual void operate() = 0;
- };
- /**
- * @brief 开后门(找乔国老帮忙,免杀刘备)
- */
- class Backdoor : public Strategy
- {
- public:
- void operate();
- };
- /**
- * @brief 开绿灯(找吴国太帮忙,放行)
- */
- class Greelight : public Strategy
- {
- public:
- void operate();
- };
- /**
- * @brief 断后(孙夫人断后)
- */
- class BlockEnemy : public Strategy
- {
- public:
- void operate();
- };
- /**
- * 锦囊(存放妙计)
- */
- class Content
- {
- public:
- Content()
- {
- }
- Content(Strategy * stra)
- {
- strat = stra;
- }
- ~Content()
- {
- delete strat;
- }
- public:
- void operate()
- {
- strat->operate();
- }
- private:
- Strategy * strat;
- };
- #endif // _STRTEGY_H_
点击(此处)折叠或打开
- #include "strtegy.h"
- #include <iostream>
- using namespace std;
- void Backdoor::operate()
- {
- cout << "找乔国老帮忙,开后门!" << endl;
- }
- void Greelight::operate()
- {
- cout << "找吴国太开绿灯放行!" << endl;
- }
- void BlockEnemy::operate()
- {
- cout << "孙夫人断后!" << endl;
- }
点击(此处)折叠或打开
- #include <iostream>
- #include "strtegy.h"
- using namespace std;
- /**
- * @brief 模板锦囊(放妙计)
- */
- template <typename T>
- class ContentT
- {
- public:
- void operate()
- {
- strat.operate();
- }
- private:
- T strat;
- };
- int main()
- {
- #if 1
- Content * cont; /// < 得到锦囊
-
- cont = new Content(new Backdoor());
- cont->operate(); /// < 开后门
- delete cont;
- cont = new Content(new Greelight());
- cont->operate(); /// < 开绿灯
- delete cont;
- cont = new Content(new BlockEnemy());
- cont->operate(); /// < 段后
- delete cont;
- #endif
- 策略模式(Strategy Pattern)
- 策略模式(Strategy Pattern)
- 策略模式 Strategy Pattern
- 策略模式(Strategy Pattern)
- 策略模式(Strategy Pattern)
- Strategy Pattern (策略模式)
- 策略模式 Strategy Pattern
- 策略模式【Strategy Pattern】
- 策略模式(Strategy Pattern)
- Strategy Pattern(策略模式)
- 策略模式(STRATEGY PATTERN )
- 策略模式(Strategy Pattern)
- 策略模式Strategy Pattern
- 策略模式(Strategy Pattern)
- 策略模式【Strategy Pattern】
- 策略模式【Strategy Pattern】
- 策略模式Strategy Pattern
- Strategy Pattern策略模式
- iPhone hello world
- 序列化-理解readResolve()
- 可以删除的安卓Android系统自带程序列表
- 应用程序池与应用程序域的区别(Different between Application Pool and AppDomain )
- objective-c 中代码块(blocks)
- 策略模式(STRATEGY PATTERN )
- c#解析json文件的方法
- 伤感日志_爱情还有另外一个名字叫寂寞
- 别人的笔记
- 代理模式(PROXY PATTERN)
- blocks实践指南
- JS中的event 对象详解
- SQLite与AutoCompleteTextView结合使用
- [HDU4432][TJ02B]Sum of divisors