我正在尝试删除代码重复。我有两个类做几乎相同的事情,但类型不同。
因此,我根据成员的类型创建了一个类模板,并添加了特征类来查找另一个成员的相应类型。
现在我刚刚发现,对于我的一种类型,有一个枚举成员用于在一个或两个地方调用不同的函数。
所以我的特征结构不再起作用,因为它的模板参数是成员的类型,但现在我需要两个不同的专业化,具体取决于另一个成员。
总而言之,我现在拥有三组代码,它们的功能基本相同,我可以使用模板进行重构,但没有类型可以作为特化的基础。
我应该为这三种类型创建一个枚举并将其用作模板参数还是有不同的规范解决方案?
编辑:代码示例!结果很长,但我想要一些可以编译的东西。
// original state
#include <iostream>
#include <string>
class Foo {
public:
std::string GetFooAttributes() {
return "<Foo attributes>";
}
};
class Bar {
public:
std::string GetSomeAttributes() {
return "<Some Bar attributes>";
}
std::string GetOtherAttributes() {
return "<Other Bar attributes>";
}
};
enum BarAttrType { SomeBarAttrs, OtherBarAttrs };
class FooLogger {
Foo mFoo;
public:
FooLogger(Foo foo) : mFoo(foo) {};
/* a lot of code */
void log() {
std::cout << mFoo.GetFooAttributes() << std::endl;
}
};
class BarLogger {
Bar mBar;
BarAttrType mAttrType;
public:
BarLogger(Bar bar, BarAttrType attrType) : mBar(bar), mAttrType(attrType) {};
/* a lot of code that looks pretty much like in FooLogger */
void log() {
if(mAttrType) {
std::cout << mBar.GetOtherAttributes() << std::endl;
} else {
std::cout << mBar.GetSomeAttributes() << std::endl;
}
}
};
// current template solution
template <typename LOGOBJECT>
std::string GetAttributesHelper(LOGOBJECT logObject) {};
template <>
std::string GetAttributesHelper<Foo>(Foo foo) {
return foo.GetFooAttributes();
}
template <>
std::string GetAttributesHelper<Bar>(Bar bar) {
return bar.GetSomeAttributes();
/* return bar.GetOtherAttributes ...sometimes */
}
template <typename LOGOBJECT>
class Logger {
LOGOBJECT mLogObject;
public:
Logger(LOGOBJECT logObject) : mLogObject(logObject) {};
/* other code that is pretty similar in all cases */
void log() {
std::cout << GetAttributesHelper(mLogObject) << std::endl;
}
};
int main(int argc, char* argv[])
{
Foo myFoo;
Bar myBar;
// old solution
FooLogger fooLogger(myFoo);
fooLogger.log();
BarLogger someBarLogger(myBar, SomeBarAttrs);
someBarLogger.log();
BarLogger otherBarLogger(myBar, OtherBarAttrs);
otherBarLogger.log();
// new solution
Logger<Foo> tFooLogger(myFoo);
tFooLogger.log();
Logger<Bar> tSomeBarLogger(myBar);
tSomeBarLogger.log();
//Logger<Bar> otherBarLogger(myBar, OtherBarAttrs); // PROBLEM!
//otherBarLogger.log();
return 0;
}