策略模式

来源:互联网 发布:the greedy python 编辑:程序博客网 时间:2024/06/03 18:25

前言

在《Effective c++》第35条例中,了解到有关策略模式的设计方法,在此梳理一遍策略模式的相关内容。

策略的设计理念

策略模式定义了算法族,分别封装起来,让他们之间可以相互替换,此模式让算法的变化独立于使用算法的客户。
在“会飞的鸭子问题中”,由于橡皮鸭不会飞而带来的问题导致不能将所有鸭子的行为都封装在“鸭子”这个超类里,可以利用策略模式将鸭子的行为从中提取出来封装成接口,进而在运行时不同鸭子表现出不同的行为。
在《Effective c++》的条款35中有提到“人物健康指数的计算与人物类型无关”,与NVI手法(用一个non-virtual函数接口来调用相应的priavte virtual函数算法,即相当于嵌入了一层装饰参考模板方法模式不同的是Strategy定义一系列的算法,把它们一个个封装起来,并且使它们可相互替换。
策略模式将算法的定义部分即客户所使用的接口与算法的实现部分解耦,使得算法可独立于使用它的客户而变化。相比于通过组合的方式来获得的算法之间的相互替换(硬编码),策略模式能提供更好的可维护性以及可拓展性。

适用场合

  1. 许多相关的类仅仅是行为有异。“策略”提供了一种用多个行为中的一个行为来配置一个类的方法
  2. 需要使用一个算法的不同变体
  3. 算法使用客户不应该知道的数据。可使用策略模式以避免暴露复杂的、与算法相关的数据结构
  4. 一个类定义了多种行为,并且这些行为在这个类的操作中以多个条件语句的形式出现。将相关的条件分支移入它们各自的Strategy类中以替代这些条件语句

实现

UML类图

这里写图片描述

代码实现

#include <iostream>using namespace std;// The abstract strategyclass Strategy{public:     virtual void AlgorithmInterface() = 0;};class ConcreteStrategyA : public Strategy{public:     void AlgorithmInterface()     {          cout<<"I am from ConcreteStrategyA."<<endl;     }};class ConcreteStrategyB : public Strategy{public:     void AlgorithmInterface()     {          cout<<"I am from ConcreteStrategyB."<<endl;     }};class ConcreteStrategyC : public Strategy{public:     void AlgorithmInterface()     {          cout<<"I am from ConcreteStrategyC."<<endl;     }};class Context{public:     Context(Strategy *pStrategyArg) : pStrategy(pStrategyArg)     {     }     void ContextInterface()     {          pStrategy->AlgorithmInterface();     }private:     Strategy *pStrategy;};int main(){     // Create the Strategy     Strategy *pStrategyA = new ConcreteStrategyA;     Strategy *pStrategyB = new ConcreteStrategyB;     Strategy *pStrategyC = new ConcreteStrategyC;     Context *pContextA = new Context(pStrategyA);     Context *pContextB = new Context(pStrategyB);     Context *pContextC = new Context(pStrategyC);     pContextA->ContextInterface();     pContextB->ContextInterface();     pContextC->ContextInterface();     if (pStrategyA) delete pStrategyA;     if (pStrategyB) delete pStrategyB;     if (pStrategyC) delete pStrategyC;     if (pContextA) delete pContextA;     if (pContextB) delete pContextB;     if (pContextC) delete pContextC;}
0 0
原创粉丝点击