设计模式(21)-行为型模式-Strategy模式

别名:Policy

3.9.1功能

• 许多相关的类仅仅是行为有异。 “策略”提供了一种用多个行为中的一个行为来配置一个类的方法。

•一个类定义了多种行为,  并且这些行为在这个类的操作中以多个条件语句的形式出现。将相关的条件分支移入它们各自的S t r a t e g y类中以代替这些条件语句。

3.9.2 结构


•  S t r a t e g y( 策略,如C o m p o s i t o r )
— 定义所有支持的算法的公共接口。C o n t e x t使用这个接口来调用某C o n c r e t e S t r a t e g y定义的算法。

•  C o n c r e t e S t r a t e g y(具体策略,如Si m p l e C o m p o s i t o r, Te X C o m p o s i t o r, ArrayCompositor)
— 以St r a t e g y接口实现某具体算法。

•C o n t e x t( 上下文,如C o m p o s i t i o n )
— 用一个Co n c r e t e S t r a t e g y对象来配置。
— 维护一个对S t r a t e g y对象的引用。
— 可定义一个接口来让S t a t e g y访问它的数据。

3.9.3 协作

•S t r a t e g y和Co n t e x t相互作用以实现选定的算法。当算法被调用时, Context 可以将该算法所需要的所有数据都传递给该S t a t e g y。或者,C o n t e x t可以将自身作为一个参数传递给St r a t e g y操作。这就让St r a t e g y在需要时可以回调C o n t e x t。

•C o n t e x t将它的客户的请求转发给它的St r a t e g y。客户通常创建并传递一个Co n c r e t e S t r a t e g y对象给该Co n t e x t;这样,  客户仅与C o n t e x t交互。通常有一系列的 C o n c r e t e S tr a t e g y类可供客户从中选择。

3.9.4 C++代码示例

代码片断1:strategy.h

//strategy.h

#ifndef _STRATEGY_H_

#define _STRATEGY_H_

classStrategy

{

public:

       Strategy();

       virtual ~Strategy();

       virtual void AlgrithmInterface() = 0;

protected:

private:

};

class ConcreteStrategyA :public Strategy

{

public:

       ConcreteStrategyA();

       virtual ~ConcreteStrategyA();

       void AlgrithmInterface();

protected:

private:

};

class ConcreteStrategyB :public Strategy

{

public:

       ConcreteStrategyB();

       virtual ~ConcreteStrategyB();

       void AlgrithmInterface();

protected:

private:

};

#endif //~_STRATEGY_H_

 

//Strategy.cpp

#include"Strategy.h"

#include<iostream>

using namespace std;

Strategy::Strategy()

{

}

Strategy::~Strategy()

{

       cout << "~Strategy....."<< endl;

}

void Strategy::AlgrithmInterface()

{

}

ConcreteStrategyA::ConcreteStrategyA()

{

}

ConcreteStrategyA::~ConcreteStrategyA()

{

       cout << "~ConcreteStrategyA....." << endl;

}

void ConcreteStrategyA::AlgrithmInterface()

{

       cout << "test

              ConcreteStrategyA....."<<endl;

}

ConcreteStrategyB::ConcreteStrategyB()

{

}

ConcreteStrategyB::~ConcreteStrategyB()

{

       cout << "~ConcreteStrategyB....." << endl;

}

void ConcreteStrategyB::AlgrithmInterface()

{

       cout << "test

              ConcreteStrategyB....."<<endl;

}

 

//Context.h

#ifndef _CONTEXT_H_

#define _CONTEXT_H_

classStrategy;

/**

*这个类是Strategy模式的关键,也是Strategy

模式和Template模式的根本区别所在。

*Strategy通过组合(委托)方式实现算法

(实现)的异构,而Template 模式则采取的

是继承的方式

*这两个模式的区别也是继承和组合两种实

现接口重用的方式的区别

*/

classContext

{

public:

       Context(Strategy* stg);

       ~Context();

       void DoAction();

protected:

private:

       Strategy* _stg;

};

#endif //~_CONTEXT_H_

 

//Context.cpp

#include"Context.h"

#include"Strategy.h"

#include<iostream>

using namespace std;

Context::Context(Strategy*stg)

{

       _stg = stg;

}

Context::~Context()

{

       if (!_stg)

              delete _stg;

}

void Context::DoAction()

{

       _stg->AlgrithmInterface();

}

 

//main.cpp

#include"Context.h"

#include"Strategy.h"

#include<iostream>

using namespace std;

int main(int argc, char*argv[])

{

       Strategy* ps;

       ps = newConcreteStrategyA();

       Context*pc = new Context(ps);

       pc->DoAction();

       if (NULL != pc)

              delete pc;

       return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值