您的位置:首页 > 其它

设计模式那点事--策略模式

2015-06-24 19:05 351 查看
概念:

策略模式定义了一系列的算法,分别封装起来,让它们之间可以相互替换。此模式让算法的变化,不会影响到使用算法的客户。策略,实质上指的是算法。

例子:

一个鲜活简单的例子总能让人轻松地理解晦涩的概念。我们来看看一个关于汽车价格的策略模式。

我们知道,汽车的品牌和质量,决定了它的价格。就像宝马(BMW),法拉利(Ferrali)和奔驰(Benz)三辆汽车,它们的价格肯定是不一样的。那如果想要知道它的价格的话,可以询问销售人员等等。但是在计算机里,我们可不能直接问销售人员啊!

对于它们来说,虽然各自价格不同,是不同的算法,但是获取价格却是一种公共操作(策略模式要求的就是要封装变化的算法)。于是,可以创建一个抽象父类汽车类(Car),声明一个获取价格virtual GetPrice函数的接口。然后创建三个子类:BMW,Ferrali和Benz,分别继承于Car。通过继承关系,每个子类可以改写父类的GetPrice函数,然后在客户端中通过调用不同的汽车子类算法来获取子类汽车价格。

总结一下:

1、我们把父类Car当成是抽象策略类,提供了一个获取价格的GetPrice接口;

2、每种包含获取价格GetPrice的汽车类为具体策略类(每种牌子的车价格不一致,导致不同计算算法),要重写父类Car的GetPrice函数;

3、建立一个环境上下文类PriceContext,维护一个对父类Car对象(指针对象才能实现多态)的引用,最后给客户端调用具体的策略类对象。

UML图:



代码:

#include <iostream>
using namespace std;

class Car
{
public:
float m_fPrice;

public:
virtual float GetPrice()
{
return m_fPrice*1;
}
};

class BMW:public Car
{
public:
float GetPrice()
{
return m_fPrice*3;
}
};

class Ferrali:public Car
{
public:
float GetPrice()
{
return m_fPrice*11;
}
};

class Benz:public Car
{
public:
float GetPrice()
{
return m_fPrice*6;
}
};

class PriceContext
{
public:
int m_iFlag;

private:
Car* m_cCar;

public:
PriceContext(Car* cCar):m_cCar(cCar)
{
//this->m_cCar = cCar;
}

float GetPriceContext()
{
m_cCar->m_fPrice = 10000;
return(m_cCar->GetPrice());
}
};

int main()
{
float fPrice=0.0;
int iTag=0;

cout<<"----策略模式开始----"<<endl;
cout<<"BMW:1,Ferrali:2,Benz:3"<<endl;
cout<<"请输入您想要查询的汽车价格:";
cin>>iTag;

PriceContext* priceContext;

switch (iTag)
{
case 1:
priceContext = new PriceContext(new BMW);
break;
case 2:
priceContext = new PriceContext(new Ferrali);
break;
case 3:
priceContext = new PriceContext(new Benz);
break;
default:
priceContext = new PriceContext(new Car);
break;
}

fPrice = priceContext->GetPriceContext();

delete priceContext;
priceContext = NULL;

cout<<"价格为:"<<fPrice<<endl;
cout<<"----策略模式结束----"<<endl;

return 1;
}


策略模式和简单工厂模式相结合

简单工厂模式中,对象的动态创建判断放在了工厂类中。而基本的策略模式客户端还是要进行算法判断和对象创建。因此可模仿简单工厂,把算法的判断移到环境上下文类中,尽量减少客户端职责,降低耦合性。其中相对简单工厂模式来说,客户端中连基类都不出现,更加地隐藏算法的具体实现细节。

代码:

class PriceContext
{
public:
int m_iFlag;

private:
Car* m_cCar;

public:
PriceContext(int iFlag)  //构造中不再穿对象,传标识
{
switch (iFlag)
{
case 1:
m_cCar = new BMW;
break;
case 2:
m_cCar = new Ferrali;
break;
case 3:
m_cCar = new Benz;
break;
default:
m_cCar = new Car;
break;
}
}

float GetPriceContext()
{
m_cCar->m_fPrice = 10000;
return(m_cCar->GetPrice());
}
};

int main()
{
float fPrice=0.0;
int iTag=0;

cout<<"----策略模式开始----"<<endl;
cout<<"BMW:1,Ferrali:2,Benz:3"<<endl;
cout<<"请输入您想要查询的汽车价格:";
cin>>iTag;

PriceContext* priceContext = new PriceContext(iTag);

fPrice = priceContext->GetPriceContext();

delete priceContext;
priceContext = NULL;

cout<<"价格为:"<<fPrice<<endl;
cout<<"----策略模式结束----"<<endl;

return 1;
}


总结:

1、策略模式定义了一系列的算法,从概念上看,所有算法完成的都是相同的操作,只是表现行为不同。通过相同的方式调用所有的算法(使用多态),减少了算法类与使用算法类之间的耦合;

2、对客户隐藏具体策略(算法)的实现细节,彼此完全独立;

3、策略模式简化了单元测试,每个算法都有自己的类,可以通过自己的接口单独测试;

4、客户端必须知道所有的策略类,并自行决定使用哪一个策略类。这就意味着客户端必须理解这些算法的区别,以便适时选择恰当的算法类。换言之,策略模式只适用于客户端知道所有的算法或行为的情况。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: