1

我正在编写一个类,我希望有一些成员方法与它们相关联的一些数据,特别是它们需要使用的机器人的哪些机械系统。我想我可以把它们写成函子,像这样(这不是我的实际代码):

class MyRobot : public Robot {
public:
  MyRobot();
  void runRobot();
private:
  Command do_something_,
          do_another_thing_;
}

do_something_然后在构造函数中使用 lambda进行初始化,例如:

do_something_( [] {
  do_first_thing();
  do_second_thing();
} );

然后告诉do_something_它有什么要求:

do_something_.requires( system_a );
do_something_.requires( system_b );

runRobot()会告诉机器人的调度程序执行命令:

void MyRobot::runRobot() {
  scheduler.add(do_something_);
  scheduler.add(do_another_thing_);
}

但我开始意识到,随着命令数量的增加,构造函数MyRobot将变得越难管理,因为每个命令都将在此处定义其主体。我可以为每个命令创建一个相应的私有方法,并使用函数指针而不是 lambda 来初始化它们,但这似乎更加复杂。我还可Command以为每个特定命令进行子类化,从而将正文和要求放在一个单独的文件中,但这对于一个相当简单的任务来说感觉像是很多开销。有没有我不知道的好方法?

4

1 回答 1

0

您可以定义Command该类以获取std::function“需求”的初始值设定项列表,就像您拥有它们一样。然后,您可以制作它们自己的私有成员函数,而不是使用 lambda,do_something这样do_another_thing您就不必在构造函数中定义它们的主体。最后,在构造函数中,您可以Command通过将私有成员函数与当前 MyRobot 实例的 this 指针绑定来构造实例,同时还给它们一个需求列表。Command对象应该能够修改MyRobot实例的私有状态。下面是一个例子。另请参阅示例输出

#include <functional>
#include <iostream>
#include <vector>

enum System { SYS_A, SYS_B, SYS_C };

class Command {
public:
  typedef std::function<void()> FuncType;

  Command(FuncType func, std::initializer_list<System> requirements)
  :func_(func), requirements_(requirements) { }

  void operator()() {
    std::cout << "Executing Command:" << std::endl;
    for ( System s : requirements_ )
      std::cout << "   REQUIRES " << static_cast<int>(s) << std::endl;
    func_();
  }

private:
  FuncType            func_;
  std::vector<System> requirements_;
};

class Scheduler {
public:
  void add(Command c) {
    c();
  }
};

class Robot {
public:
  Robot()
  :do_something_    (std::bind(&Robot::do_something, this),     {SYS_A, SYS_B}),
   do_another_thing_(std::bind(&Robot::do_another_thing, this), {SYS_A, SYS_C}) { }

  void runRobot() {
    s_.add(do_something_);
    s_.add(do_another_thing_);
  }

private:
  void do_first_thing()  { std::cout << "   FIRST THING!" << std::endl;  }
  void do_second_thing() { std::cout << "   SECOND THING!" << std::endl; }
  void do_third_thing()  { std::cout << "   THIRD THING!" << std::endl;  }

  void do_something()     { do_first_thing(); do_second_thing(); }
  void do_another_thing() { do_first_thing(); do_third_thing();  }

  Command   do_something_;
  Command   do_another_thing_;
  Scheduler s_;
};

int main(int, char**) {
  Robot().runRobot();
}
于 2013-06-04T08:52:56.983 回答