大话设计模式--策略模式 strategy -- C++实现实例
2013-10-08 09:30
513 查看
1. 策略模式:
它定义了算法家族, 分别封装起来,使他们之间可以相互替换,此模式让算法变化, 不会影响到使用算法的客户。
用相同的方法调用不同的算法,减少各种算法类与使用算法类之间的耦合。
实例中策略模式与工厂模式相结合,不同之处在于 main函数的使用体现,
如果用但纯的工厂模式则必须知道两个类, 但是两种模式结合后,客户端只需要知道一个类 context .
另外,工厂模式生产的是具体的算法, 而策略模式则是能通过不同策略调用不同算法,直接获取需要的结果。
实例:
strategy.h 策略基类
strategy.cpp
concretestrategy.h 具体策略实现, 省篇幅,写在一起
concretestrategy.cpp
context.h 上下文类
context.cpp
main.cpp
它定义了算法家族, 分别封装起来,使他们之间可以相互替换,此模式让算法变化, 不会影响到使用算法的客户。
用相同的方法调用不同的算法,减少各种算法类与使用算法类之间的耦合。
实例中策略模式与工厂模式相结合,不同之处在于 main函数的使用体现,
如果用但纯的工厂模式则必须知道两个类, 但是两种模式结合后,客户端只需要知道一个类 context .
另外,工厂模式生产的是具体的算法, 而策略模式则是能通过不同策略调用不同算法,直接获取需要的结果。
实例:
strategy.h 策略基类
#ifndef STRATEGY_H #define STRATEGY_H class Strategy { public: Strategy(); void virtual AlgorithmInterface(); }; #endif // STRATEGY_H
strategy.cpp
#include "strategy.h" Strategy::Strategy() { } void Strategy::AlgorithmInterface() { }
concretestrategy.h 具体策略实现, 省篇幅,写在一起
#ifndef CONCRETESTRATEGY_H #define CONCRETESTRATEGY_H #include "strategy.h" class ConcreteStrategyA : public Strategy { public: void AlgorithmInterface(); }; class ConcreteStrategyB : public Strategy { public: void AlgorithmInterface(); }; class ConcreteStrategyC : public Strategy { public: void AlgorithmInterface(); }; #endif // CONCRETESTRATEGY_H
concretestrategy.cpp
#include "concretestrategy.h" #include <stdio.h> void ConcreteStrategyA::AlgorithmInterface() { printf("ConcreteStrategyA\n"); } void ConcreteStrategyB::AlgorithmInterface() { printf("ConcreteStrategyB\n"); } void ConcreteStrategyC::AlgorithmInterface() { printf("ConcreteStrategyC\n"); }
context.h 上下文类
#ifndef CONTEXT_H #define CONTEXT_H #include "strategy.h" #include "concretestrategy.h" #include <string> using namespace std; class Context { public: Context(string type); Strategy* strategy; void contextInterFace(); }; #endif // CONTEXT_H
context.cpp
#include "context.h" Context::Context(string type) { if( type == "A" ) strategy = new ConcreteStrategyA(); else if( type == "B" ) strategy = new ConcreteStrategyB(); else if( type == "C" ) strategy = new ConcreteStrategyC(); } void Context::contextInterFace() { strategy->AlgorithmInterface(); }
main.cpp
#include <iostream> #include "context.h" using namespace std; int main() { cout << "strategy test!" << endl; Context *context = new Context("C"); context->contextInterFace(); return 0; }
相关文章推荐
- 大话设计模式--策略模式 strategy -- C++实现实例
- 《大话设计模式》- 策略模式 - C++实现
- HeadFirst设计模式 之 C++实现(一):Strategy(策略模式) && 初入设计模式殿堂
- 大话设计模式--命令模式 Command -- C++实现实例
- 大话设计模式--观察者模式 Observer -- C++ 实现实例
- 大话设计模式--解释器模式 interpreter -- C++实现实例
- 大话设计模式--观察者模式 Observer -- C++ 实现实例
- 设计模式C++实现(2)——策略模式(strategy)
- 大话设计模式--桥接模式 Bridge -- C++实现实例
- 大话设计模式--命令模式 Command -- C++实现实例
- 大话设计模式--职责连模式 Chain of Resposibility -- C++实现实例
- C++实现策略(Strategy)模式
- 大话设计模式--解释器模式 interpreter -- C++实现实例
- 大话设计模式--桥接模式 Bridge -- C++实现实例
- 大话设计模式--职责连模式 Chain of Resposibility -- C++实现实例
- 大话设计模式 第二章 策略模式 C++实现
- 大话设计模式c++实现2——策略模式 Strategy Pattern
- C++实现策略(Strategy)模式
- 大话设计模式--工厂模式 factory -- C++实现实例
- 设计模式系列1-----C++实现策略模式(Strategy)