C++ 为实现接口的类避免多个几乎相同的声明
我有一个名为C++ 为实现接口的类避免多个几乎相同的声明,c++,class,declaration,dry,class-design,C++,Class,Declaration,Dry,Class Design,我有一个名为Command的抽象基类,它充当可放入队列的命令的接口: class Command { public: Command(Dependency1& d1, Dependency2& d2); //...Irrelevant code removed for simplicity... private: //Implementations do their work in their override of operator()
Command
的抽象基类,它充当可放入队列的命令的接口:
class Command
{
public:
Command(Dependency1& d1, Dependency2& d2);
//...Irrelevant code removed for simplicity...
private:
//Implementations do their work in their override of operator()
virtual void operator()() = 0;
};
然后在头文件中有实现的声明:
class FooCommand : public Command
{
public:
using Command::Command;
private:
void operator()() override;
};
class BarCommand : public Command
{
public:
using Command::Command;
private:
void operator()() override;
};
class BazCommand : public Command
{
public:
using Command::Command;
private:
void operator()() override;
};
//...And many more...
现在我有一长串几乎相同的类声明,只是名称有点不同。除了C风格的宏之外,还有什么更好的方法来清理它呢?这完全取决于您需要在
操作符()中执行什么操作
如果您不需要访问命令的状态,那么您可以选择向构造函数传递一个可调用的。就像这里:
class CallCommand : public Command {
std::function<void()> f;
public:
CallCommand(Dependency1& d1, Dependency2& d2, std::function<void()> f) : Command(d1,d2), f(f) {
}
private:
void operator()() override { f(); }
};
但是,如果您需要访问类上下文,那么我很抱歉,没有办法像您那样进行真正的重写(对于样板代码来说,宏是一个糟糕的工作环境) 答案与前面的答案类似,但不使用继承
你可能不需要继承。相反,您可以使用std::function
或lambda
表达式。例如,见:
class Command
{
private:
std::function<void()> f;
public:
Command(Dependency1& d1, Dependency2& d2, std::function<void()> f): d1(d1), d2(d2), f(f) {}
void operator()() {
f();
}
};
在c++11
之前,您可以通过为每个函数创建函数指针来避免继承,并将这些指针传递给命令
构造函数。是否需要使用继承?那么函数指针、lambda表达式或std::function
呢?
class Command
{
private:
std::function<void()> f;
public:
Command(Dependency1& d1, Dependency2& d2, std::function<void()> f): d1(d1), d2(d2), f(f) {}
void operator()() {
f();
}
};
Command call1(0, 0, []() {
std::cout << "call1" << std::endl;
});
Command call2(0, 0, []() {
std::cout << "call2" << std::endl;
});
std::vector<Command> v;
v.emplace_back(call1);
v.emplace_back(call2);
for (size_t i = 0; i < v.size(); i++) {
v[i]();
}