c++ - 如何实例化基于输入的策略模式

标签 c++ design-patterns pattern-matching

策略模式的实例化通常被示例完全忽略。假设有一个输入定义要使用哪个类。我们会得到一些类似的东西:

class Strategy {
    Strategy(){}
    virtual void runAlgorithm() = 0;
};

class A : public Strategy {
    A () {}
    static bool isA(char input){ return input == 'A'; }
    void runAlgorithm { /* Do A algorithm */ }
};

class B : public Strategy {
    B () {}
    static bool isB(char input){ return input == 'B'; }
    void runAlgorithm { /* Do B algorithm */ }
};

// Other algorithms

Strategy* createStrat(char input){
    Strategy* instance;

    // Define which algorithm to use
    if (A::isA(input)) {
        instance = A();
    } else if (B::isB(input)) {
        instance = B();
    } ...

    // Run algorithm
    instance.runAlgorithm();

    return instance;
}

正如你所看到的,如果我们有多种不同的算法,这个 if/switch 可能会变得相当大。是否有一种模式可以使该代码更易于人为解析(即 for 循环和调用),而无需添加对数组?这个问题也可以扩展到“如何实例化基于输入的策略模式?”

不要限制于此代码,因为它只是一个示例。

最佳答案

好吧,如果您提前知道所有策略,您可以使用非常简单的元编程递归来自动展开 if-else 链。我们开始吧:

#include <string_view>
#include <iostream>
#include <exception>
#include <memory>

struct Strategy {
    Strategy(){}
    virtual void runAlgorithm() = 0;
};

struct A : public Strategy {

    A () {std::cout << "Creating A" << std::endl;}

    static constexpr std::string_view id(){
        return std::string_view("A");
    }

    void runAlgorithm() { /* Do A algorithm */ }
};

struct B : public Strategy {

    B () {std::cout << "Creating B" << std::endl;}

    static constexpr std::string_view id(){
        return std::string_view("B");
    }

    void runAlgorithm() { /* Do B algorithm */ }
};

struct C : public Strategy {

    C () {std::cout << "Creating C" << std::endl;}

    static constexpr std::string_view id(){
        return std::string_view("C");
    }

    void runAlgorithm() { /* Do C algorithm */ }
};

// the if else chains are constructed by recursion
template <class Head, class... Tail>
struct factory {

  static std::unique_ptr<Strategy> call(std::string id) {
      if(Head::id() == id) return std::make_unique<Head>();
      else return factory<Tail...>::call(id);
  }
};

// specialization to end the recursion
// this throws an exception, but you can adapt it to your needs
template <class Head>
struct factory<Head> {

  static std::unique_ptr<Strategy> call(std::string id) {
      if(Head::id() == id) return std::make_unique<Head>();
      else throw std::invalid_argument("The strategy id you selected was not found.");
  }
};

// here is your factory which can create instances of A,B,C based only on the runtime id
using my_factory = factory<A,B,C>;

int main() {

    auto Astrategy = my_factory::call("A");
    auto Bstrategy = my_factory::call("B");
    auto Cstrategy = my_factory::call("C");
    my_factory::call("D"); // exception thrown

}

实时代码 here

编辑

按照 Jarod42 的建议进行编辑以考虑智能指针和错误检查。

关于c++ - 如何实例化基于输入的策略模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51493703/

相关文章:

design-patterns - 访客vs仆人vs命令模式

php - Demeter 法则非常令人困惑,因为看起来我永远无法编写返回对象的方法

c++ - 在不公开函数的情况下向另一个类授予对函数的访问权限

macros - 宏可以扩展为模式组合吗?

templates - 为什么在 play 2 scala 模板中构造的 html 会生成空的 Html 案例类

c++ - 处理 utf8 编码的 char* 数组

c++ - 没有从类型 bankAccount 的返回值到函数返回类型 int 的可行转换

c++ - 使用基本数组来实现兼容性

c++ - SFINAE: std::enable_if 作为函数参数

Scala模式匹配: Are parametrized extractor objects possible?