对于这个有点冗长的问题,请提前接受我的道歉。这是我能想出的最小的独立示例...我很确定必须有一些明显/很好/整洁的解决方案来解决这个问题,但我目前看不到它。
好吧,问题来了:想象一下下面的情况(注意,代码的可编译版本可在 http://goo.gl/dhRNex 获得)。假设
struct Thing1 {
public:
void bar(class Implementation1 &i) {
i.baz();
}
// ...various other methods like bar()
};
struct Thing2 {
public:
void bar(class Implementation2 &i) {
i.qux();
}
// ...various other methods like bar()
};
给出。不幸的是,这些类是固定的,即不能更改/重构。
然而,Implementation1
和 Implementation2
是多变的。这两个类共享很多相似的代码,因此将共享代码放在一个公共(public)基类中似乎很自然。但是,代码取决于 Thing
的类型使用过,但 Thing1
没有公共(public)基类和 Thing2
,因此使用模板似乎也很自然。因此,我为基类想出了以下解决方案
template<class T, class S>
struct ImplementationBase {
public:
S *self;
void foo() {
T thing;
thing.bar(*self);
}
// ...lots more shared code like foo()
};
及具体实现
struct Implementation1 : public ImplementationBase<class Thing1, class Implementation1> {
public:
Implementation1() {
self = this;
}
void baz() {
std::cout << "Qux!" << std::endl;
}
};
struct Implementation2 : public ImplementationBase<class Thing2, class Implementation2> {
public:
Implementation2() {
self = this;
}
void qux() {
std::cout << "Qux!" << std::endl;
}
};
理想情况下,我们会使用 this
而不是 self
在 foo
,但问题是 this
类型为 ImplementationBase<class Thing1, class Implementation1>
,但是Implementation1
是必须的。显然,整个事情一团糟,Implementation
和 Thing
类耦合得太紧密,但如果不能重构 Thing
,我看不到一个简单的出路。类。所以,最后,我的问题是:
- 有没有比使用
self
更好的替代方法?上面的技巧? - 有没有一种设计可以更好地解决这个问题? (我有一种感觉,有,但我遗漏了一些明显的东西)
如果您已经读到这里,非常感谢您花时间阅读整个故事,对于这个冗长的问题,我再次表示歉意。
最佳答案
您已经在使用 CRTP,因此您根本不需要 self:
template<class T, class S>
struct ImplementationBase {
public:
S* getThis() { return static_cast<S*>(this); }
void foo() {
T thing;
thing.bar(*getThis());
}
// ...lots more shared code like foo()
};
关于c++ - 当只能更改一侧时重构紧密耦合的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27494072/