C++ 策略模式

作用:定义了算法家族,分别封装起来,让他们之间可以互相替换,此模式让算法的变化,不会影响到使用算法的客户。

UML图:



Strategy模式将逻辑(算法)封装到一个类(Context)里面,通过组合的方式将具体算法的实现在组合对象中实现,再通过委托的方式将抽象接口的实现委托给组合对象实现

将算法的逻辑抽象接口(DoAction)封装到一个类中(Context),再通过委托的方式将具体的算法实现委托给具体的Strategy类来实现(ConcreteStrategeA类)

Stragegy类,定义所有支持的算法的公共接口
ConcreteStrategy,封装了具体的算法或行为,继承于Strategy
Context,用一个ConcreteStrategy来配置,维护一个对Strategy对象的引用

策略模式是一种定义一系列算法的方法,从概念上来看,所有这些算法完成的都是相同的工作,只是实现不同,它可以以相同的方式调用所有的算法,减少了各种算法类于使用算法类之间的耦合。

策略模式的Strategy类层次为Context定义了一系列的可供重用的算法或行为。继承有助于析取出这些算法中的公共功能。

策略模式的优点是简化了单元测试,因为每个算法都有自己的类,可以通过自己的接口单独测试。

策略模式就是用来封装算法的,但在实践中,我们发现可以用它来封装几乎任何类型的规则,只要在分析过程中听到需要在不同时间应用不同的业务规则,就可以考虑使用策略模式处理这种变化的可能性。

在基本的策略模式中,选择所用具体实现的职责由客户端对象承担,并转给策略模式的Context对象。这本身并没有解除客户端需要选择判断的压力。

代码如下:

#ifndef _STRATEGY_H_
#define _STRATEGY_H_

class Strategy
{
public:
    ~Strategy();
    virtual void AlgrithmInterface()=0;
protected:
    Strategy();
private:
};

class ConcreteStrategyA : public Strategy
{
public:
    ConcreteStrategyA();
    ~ConcreteStrategyA();
    virtual void AlgrithmInterface();
protected:
private:
};

class ConcreteStrategyB : public Strategy
{
public:
    ConcreteStrategyB();
    ~ConcreteStrategyB();
    virtual void AlgrithmInterface();
protected:
private:
};

/*这个类是Strategy模式的关键,
  也是Strategy模式和Template模式的根本区别所在。
  *Strategy通过“组合”(委托)方式实现算法(实现)的异构,
  而Template模式则采取的是继承的方式
  这两个模式的区别也是继承和组合两种实现接口重用的方式的区别
*/
class Context
{
public:
    Context(Strategy*);
    ~Context();
    void DoAction();
private:
    Strategy* _strategy;
};
#endif
#include "Strategy.h"
#include "iostream"

using namespace std;

Strategy::Strategy()
{}

Strategy::~Strategy()
{}

ConcreteStrategyA::ConcreteStrategyA()
{}

ConcreteStrategyA::~ConcreteStrategyA()
{}

void ConcreteStrategyA::AlgrithmInterface()
{
    cout << "ConcreteStrategyA::AlgrithmInterface" << endl;
}

ConcreteStrategyB::ConcreteStrategyB()
{}

ConcreteStrategyB::~ConcreteStrategyB()
{}

void ConcreteStrategyB::AlgrithmInterface()
{
    cout << "ConcreteStrategyB::AlgrithmInterface" << endl;
}

Context::Context(Strategy* strategy)
{
    this->_strategy = strategy;
}

Context::~Context()
{
    delete this->_strategy;
}

void Context::DoAction()
{
    this->_strategy->AlgrithmInterface();
}
#include "Strategy.h"

int main()
{
    /*
    Strategy模式和Template模式实际是实现一个抽象接口的两种方式:继承和组合之间的区别。
    要实现一个抽象接口,继承是一种方式:我们将抽象接口声明在基类中,将具体的实现放在具体子类中。
    组合(委托)是另外一种方式:我们将接口的实现放在被组合对象中,将抽象接口放在组合类中。
    这两种方式各有优缺点
    */
    //策略A与B可替换
    Strategy* pStr = new ConcreteStrategyA();
    Context* pcon = new Context(pStr);
    pcon->DoAction();

    pStr = new ConcreteStrategyB();
    pcon = new Context(pStr);
    pcon->DoAction();

    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++中的策略模式是一种行为型设计模式,它允许在运行时选择算法的不同实现。该模式定义了一组算法,将它们各自封装起来,并使它们可以互换。 在C++中,可以使用函数指针、函数对象或Lambda表达式来实现策略模式。以下是一个简单的示例: ```c++ #include <iostream> class SortingStrategy { public: virtual void sort(int* arr, int n) = 0; }; class BubbleSort : public SortingStrategy { public: void sort(int* arr, int n) { // Bubble sort implementation std::cout << "Sorting using bubble sort." << std::endl; } }; class QuickSort : public SortingStrategy { public: void sort(int* arr, int n) { // Quick sort implementation std::cout << "Sorting using quick sort." << std::endl; } }; class Sorter { private: SortingStrategy* strategy; public: void setSortingStrategy(SortingStrategy* newStrategy) { strategy = newStrategy; } void sort(int* arr, int n) { strategy->sort(arr, n); } }; int main() { int arr[] = {5, 2, 9, 3, 6}; int n = sizeof(arr) / sizeof(arr[0]); Sorter sorter; BubbleSort bubbleSort; sorter.setSortingStrategy(&bubbleSort); sorter.sort(arr, n); QuickSort quickSort; sorter.setSortingStrategy(&quickSort); sorter.sort(arr, n); return 0; } ``` 在上面的示例中,SortingStrategy是抽象策略类,BubbleSort和QuickSort是具体策略类。Sorter是使用策略模式的上下文类,它可以使用setSortingStrategy方法设置当前使用的SortingStrategy实现,并在sort方法中使用该实现对输入数组进行排序。 运行上面的代码将输出: ``` Sorting using bubble sort. Sorting using quick sort. ``` 这表明程序成功地使用了策略模式来选择不同的排序算法实现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值