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

1.什么是策略模式

策略模式属于对象行为模式,其用意是针对一组算法,将每一个算法封装到具有共同接口的独立的类中,从而使它们可以相互替换。

2.策略模式的结构

策略模式是对算法的包装,把使用算法的责任和算法本身分开,委派给不同的对象管理。

 \
 


环境(Context)角色:持有一个Strategy引用;

抽象角色(Strategy)对象:抽象角色,由一个抽象类或借口实现,它指出所有的具体策略类所需的接口;

具体角色(ConcreteStrategy)对象:包装了相关算法或行为;

优点: 
1. 提供了一种替代继承的方法,而且保持了继承的优点,比继承更独立(算法独立,可
以任意扩展) 
2. 避免程序使用多重条件转移语句,使系统更灵活,并易于扩展 3. 遵守大部分常用设计原则,高内聚,低耦合 
用工厂方法来解决。 
用途: 各个不同地区不同的纳税方法,HF中不同鸭子的方法。有多种鸭子,每个鸭子都有自
己的行为,fly,quaak之类的。行为有行为类,继承同一接口实现不同操作,以此实现算法互换。

 

缺点:

(1)客户端必须知道所有的策略类,并自行决定使用哪一个策略类。这就意味着客户端必须理解这些算法的区别,以便适时选择恰当的算法类。换言之,策略模式只适用于客户端知道算法或行为的情况。
(2)由于策略模式把每个具体的策略实现都单独封装成为类,如果备选的策略很多的话,那么对象的数目就会很可观。

 

 

#include <iostream>
using namespace std;

// The abstract strategy
class 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;
}
using namespace std;

// The abstract strategy
class 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;
}


一个飞机模拟系统的实例:

 

 

#include <iostream>

#include <string>
using namespace std;
class FlyBechavior {
public:
	virtual void Fly() = 0;
};
class SubSonicFly : public FlyBechavior {
public:
	void Fly()
	{
		cout << "亚音速飞行!" << endl;
	}
};
class SuperSonicFly : public FlyBechavior {
public:
	void Fly()
	{
		cout << "超音速飞行!" << endl;
	}
};
class TakeoffBechavior {
public:
	virtual void takeoff() = 0;
};
class VerticalTakeoff : public TakeoffBechavior {
public:
	void takeoff()
	{
		cout << "垂直起飞和";
	}
};
class LongdistanceTakeoff : public TakeoffBechavior {
public:
	void takeoff()
	{
		cout << "长距离起飞和";
	}
};
class AirCraft {
private:
	string			name;
	FlyBechavior		*flyBehavior;
	TakeoffBechavior	*takeoffBchavior;
public:
	AirCraft( string name, FlyBechavior *flyBehavior, TakeoffBechavior *takeoffBchavior )
	{
		this->name		= name;
		this->flyBehavior	= flyBehavior;
		this->takeoffBchavior	= takeoffBchavior;
	}


	void fun()
	{
		cout << name << ":";
		takeoffBchavior->takeoff();
		flyBehavior->Fly();
	}
};
class Airplane : public AirCraft {
public:
	Airplane( string name, FlyBechavior *flyBehavior, TakeoffBechavior *takeoffBchavior ) : AirCraft( name, flyBehavior, takeoffBchavior )
	{
	}
};
class Helicper : public AirCraft {
public:
	Helicper( string name, FlyBechavior *flyBehavior, TakeoffBechavior *takeoffBchavior ) : AirCraft( name, flyBehavior, takeoffBchavior )
	{
	}
};
class Fighter : public AirCraft {
public:
	Fighter( string name, FlyBechavior *flyBehavior, TakeoffBechavior *takeoffBchavior ) : AirCraft( name, flyBehavior, takeoffBchavior )
	{
	}
};
class Harrier : public AirCraft {
public:
	Harrier( string name, FlyBechavior *flyBehavior, TakeoffBechavior *takeoffBchavior ) : AirCraft( name, flyBehavior, takeoffBchavior )
	{
	}
};


int main( void )
{
	FlyBechavior		*subF		= new SubSonicFly();
	FlyBechavior		*superF		= new SuperSonicFly();
	TakeoffBechavior	*verticalt	= new VerticalTakeoff();
	TakeoffBechavior	*longdist	= new LongdistanceTakeoff();
	AirCraft		*harrier	= new Harrier( "强力战斗机", superF, verticalt );
	harrier->fun();
	delete harrier;
	return(0);
}















 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值