策略模式或函数指针

Strategy pattern or function pointer

在 C++ 中,当我有可以在运行时接受不同行为的算法时,我宁愿使用函数指针。

例如,一个绘制图表的程序有一个画线的算法,它可以接受任何函数来专门化这条线的形状。

在Java中没有函数指针,我不得不使用策略模式或反射(或其他模式)。

如何在我的程序中启用在运行时选择特殊行为? 策略模式要么是函数指针?

在Java中,函数指针是使用仿函数实现的。用一个函数创建一个接口并传递它的实例而不是函数指针。假设您的 C++ 代码如下所示:

void func(void (*f)(int par));

在 Java 中看起来像:

public interface F {
  public void f(int par);
}

void func(F f);

看看Guava的Functionclass。这对 C++ 来说也不是一个坏方法。它更具可读性,并允许用户传入具有状态的对象,而不是静态函数。

这完全取决于你用仿函数做什么。如果你想一次性使用它来做一个操作,你想使用一个模板。例如,查看任何标准算法 - 例如,std::sort:

template< class RandomIt, class Compare >
void sort( RandomIt first, RandomIt last, Compare comp );
                                          ^^^^^^^^^^^^
                                          any functor that does comparing

如果您想存储仿函数供以后使用,您需要 std::function - 类型擦除的仿函数:

class SaveFunctionForLater {
public:
    template <typename F>
    SaveFunctionForLater(F&& f)
        : func(std::forward<F>(f))
        { }

    int callMe(int i) { return func(i); }
                               ^^^^
                               use the saved one

private:
    std::function<int(int)> func; // function taking an int, returning an int
};

你会使用哪个:

int f(int i) { return 2*i; }

template <int I>
struct Constant {
    int operator()(int ) { return I; }
};

SaveFunctionForLater add1([](int i){return i+1;});    // works with lambda
SaveFunctionForLater double(f);                       // works with func ptr
SaveFunctionForLater zero(Constant<0>{});             // works with object

cout << add1.callMe(2);                  // prints 3
cout << double.callMe(double.callMe(4)); // prints 16
cout << zero.callMe(42);                 // prints 0

您绝对不想使用显式函数指针 - 这将严重限制您的 class/function 的可用性。您也希望允许人们传递对象。毕竟,这是 C++ 而不是 C!

在 C++ 中有多种实现策略模式的方法。

  1. 像在其他编程语言中一样使用 OOP 方法。
  2. 使用函数对象。
  3. 使用政策class。

这三个都可以完成工作,所以选择哪一个在很大程度上取决于您的问题。

您提到要使用策略来绘制图表。在我看来,这是一个复杂的用例,您应该选择选项一,因为您的策略对象可能会查询首选颜色等设置,您可能希望将策略存储在某个地方,以便用户可以 select 它们在组合框中。

如果您想为客户(您的同事)提供很大的灵活性,那么函数对象非常好。在我当前的项目中,我们使用函数对象在算法中进行停止标准测试。只要功能不会变得太复杂,这对于 C++11 中的 lambda 就非常好。

基于策略的方式可能是最快的,但它需要在编译时知道类型。使用它时没有 DLL 基础插件。至少在您将实现封装在 class 层次结构之前:

template <class LockPolicy>
class MyClass
{
  void do_something()
  {
    LockPolicy::lock();
    //...
    LockPolicy::unlock();
  }
};

我认为好的建议是:

  • 如果策略比较复杂,可能会查询其他系统,就用1。
  • 如果你想通过插件实现新的策略,请使用1.
  • 如果策略可以用较短的代码行表示,则使用2
  • 如果策略实现适合自由函数并且您不想引入 class 层次结构
  • ,请使用 2。
  • 如果您真的知道自己在做什么并且需要性能,请使用 3。