天天看点

c++ 设计模式——策略模式c++设计模式——策略模式

c++设计模式——策略模式

  • 定义

    策略模式定义了一系列的算法,并将每一个算法封装起来,而且使它们相互之间可以替换。策略模式让算法独立于使用它的客户而独立变化。

  • 角色
    • 抽象策略角色(strategy):抽象策略类
    • 具体策略角色(concrete strategy):封装了相关的算法和行为
    • 环境角色(context):持有一个策略类的引用,最终给客户端调用
  • 模式图
c++ 设计模式——策略模式c++设计模式——策略模式
  • 应用场景
    1. 需要使用ConcreteStrategy提供的算法。
    2. 内部维护一个strategy的实例
    3. 负责动态设置运行时strategy具体的实现算法
    4. 负责strategy之间的交互和数据传递
  • 优点
    1. 使用策略模式可以避免使用多重条件转移语句。多重转移语句不易维护。
    2. 策略模式让你可以动态的改变对象的行为,动态修改策略
  • 缺点
    1. 客户端必须知道所有的策略类,并自行决定使用哪种策略
    2. 类过多的问题。策略模式造成了很多策略类,每个具体策略类都会产生一个新类。(可以使用享元模式来克服类过多的问题)
  • 实例

    空调的三种运行模式;

    1. 冷风模式
    2. 热风模式
    3. 无风模式

    三种运行模式就是ConcreteStrategy。

    代码实现如下:

    # ifndef _STRATEGY_H_
    # define _STRATEGY_H_
    
    using namespace std;
    typedef enum WindMode{
        COLDMODE = 0, 
        WARMMODE, 
        NOMODE 
    }MODE;
    
    class Wind{
    public:
        virtual void IWind() = 0;
    };
    
    class ColdWind:public Wind{
    public:
        void IWind(){
            cout << "this is ColdWind"  << endl;      
        }
    };
    
    class WarmWind:public Wind{
    public:
        void IWind(){
            cout << "this is WarmWind"  << endl;      
        }
    };
    
    class NoWind:public Wind{
    public:
        void IWind(){
            cout << "this is NoWind"  << endl;      
        }
    };
    
    class Context{
    public:
        Wind * wind;
        Context(MODE mode){
            switch(mode){
                case COLDMODE:
                    wind = new ColdWind();
                    break;
                case WARMMODE:
                    wind = new WarmWind();
                    break;
                case NOMODE:
                    wind = new NoWind();
                    break;
                default:
                    wind = nullptr; 
            }
        }
    
        ~Context(){
            if(nullptr != wind){
                delete wind;
                wind = nullptr;
            }
        }
    };
    
    # endif
               
    测试代码如下strategy.h
    # include <iostream>
    # include "strategy.h"
    
    using namespace std;
    
    int main(int argc ,char ** argv){
        Context * cold_context = new Context(COLDMODE);     
        Context * warm_context = new Context(WARMMODE);     
        Context * no_context = new Context(NOMODE);     
    
        cold_context->wind->IWind();
        warm_context->wind->IWind();
        no_context->wind->IWind();
    
        if(nullptr != cold_context){
            delete cold_context;
            cold_context = nullptr;
        }
    
        if(nullptr != warm_context){
            delete warm_context;
            warm_context = nullptr;
        }
    
        if(nullptr != no_context){
            delete no_context;
            no_context = nullptr;
        }
    
        return 0;
    }
               
    测试结果:
    [[email protected] designMode]$ g++ strategy_test.cc -std=c++11 
    [[email protected] designMode]$ ./a.out 
    this is ColdWind
    this is WarmWind
    this is NoWind
               

继续阅读