本文主要是介绍【重读设计模式】策略模式,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
策略模式也是一个比较常见的模式,其实我们在平时工作设计中用的很多,但是可能并没有发现其就是策略模式。
使用率:使用的较多的模式之一,但是由于他会产生很多的类,有时候追求开发的速度,系统的部分采用了该模式,其他部分没有采用。
使用场景:一样事物,具有多个行为,而每个行为都有不同的行为方式,通过将行为单独成为接口类,让事物的动作委托行为类去实现。从而将事物和其行为解耦,甚至于可以在运行过程中替换。
实例:
比如CS游戏中的角色,用户进入游戏后可以选择一个角色(是警察还是土匪),角色都有三个初始状态,一个是什么样的衣服,一个是什么样的手枪,还一个是什么样的步枪。
分析:
有很多中不同的角色,所以角色是一个抽象类,每个角色都有三个状态(衣服,手枪,步枪),而每一种状态都有不同的选择,比如衣服有短袖,无袖,长袖,所以每个状态都是一个抽象类,每个角色都必须有三个状态,那么这里角色和状态又是一个has a的关系,即角色有三个成员,分别表示三个状态,另外一个特点是并且当一个角色选定后,默认的状态是固定的。另外,角色的状态应该是可以修改的,比如你捡了一把手枪,就可以替换当前默认状态下的手枪,从而运行时改变自己的默认状态,而修改之后,就会以新的状态呈现。
类关系图:
实现代码:
//============================================================================
// Name : strategy.cpp
// Author : tester
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
using namespace std;
//角色抽象类
class CRole
{
public:
int show_clothes()
{
return clothes_->display();
}
int show_handgun()
{
return handgun_->click();
}
int show_autogun()
{
return clothes_->shot();
}
int set_clothes(CClothes* other)
{
clothes_ = other;
return 0;
}
int set_handgun(CHandGun* other)
{
handgun_ = other;
return 0;
}
int set_autogun(CAutoGun* other)
{
autogun_ = other;
return 0;
}
protected:
//生命为保护的,保证不会被实例化
CRole(){};
protected:
CClothes* clothes_;
CHandGun* handgun_;
CAutoGun* autogun_;
};
//恐怖分子类
class CTerrorist : public CRole
{
public:
CTerrorist()
{
clothes_ = new CHalfClothes();
handgun_ = new CTessHandGun();
autogun_ = new CAk47();
}
};
//警察类
class CPolice : public CRole
{
public:
CPolice()
{
clothes_ = new CWholeClothes();
handgun_ = new CPoliHandGun();
autogun_ = new CM4();
}
};
//衣服和武器类
class CClothes
{
public:
int show_clothes() = 0;
};
class CHalfClothes : public CClothes
{
public:
int show_clothes()
{
printf("您穿短袖.\n");
return 0;
}
};
class CWholeClothes : public CClothes
{
public:
int show_clothes()
{
printf("您穿长袖.\n");
return 0;
}
};
class CHandGun
{
public:
int show_handgun() = 0;
};
class CTessHandGun : public CHandGun
{
public:
int show_handgun()
{
printf("您使用B05手枪.\n");
return 0;
}
};
class CPoliHandGun : public CHandGun
{
public:
int show_handgun()
{
printf("您使用B35手枪.\n");
return 0;
}
};
class CAutoGun
{
public:
int show_autogun() = 0;
};
class CAk47 : public CAutoGun
{
public:
int show_autogun()
{
printf("您使用ak47.\n");
return 0;
}
};
class CM4 : public CAutoGun
{
public:
int show_autogun()
{
printf("您使用M4.\n");
return 0;
}
};
int main() {
//cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
//测试
//1、先建立一个恐怖分子
CTerrorist* terr = new CTerrorist;
//显示其状态
terr->show_clothes();
terr->show_handgun();
terr->show_autogun();
//2、再建立一个警察
CTerrorist* police = new CPolice;
police->show_clothes();
police->show_handgun();
police->show_autogun();
//3、警察见到了一把ak47的枪
police->set_autogun(new CAk47());
police->show_autogun();
return 0;
}
可以从代码上直观的看到,将会变化的角色、状态(衣服,手枪,步枪)全部抽象,在对象和状态(或动作)使用组合,是角色和状态本身解耦,可以在运行期间动态的改变用户的状态。
前面的状态模式讲过,状态模式和策略模式实现起来有点类似,而实际上是不一样的,状态模式描述的是对象在不同的状态下相同的操作会产生不同的行为,对象的每个状态下都会有不同的操作。而策略模式描述的是对象在特定的状态下行为是固定的,用户是可以通过动态的去改变用户的状态从而改变对象的行为,但是改变之后依然是固定的。两个模式使用的场景不一样。
这篇关于【重读设计模式】策略模式的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!