c++ - 当只能更改一侧时重构紧密耦合的类

标签 c++ templates design-patterns

对于这个有点冗长的问题,请提前接受我的道歉。这是我能想出的最小的独立示例...我很确定必须有一些明显/很好/整洁的解决方案来解决这个问题,但我目前看不到它。

好吧,问题来了:想象一下下面的情况(注意,代码的可编译版本可在 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()
};

给出。不幸的是,这些类是固定的,即不能更改/重构。

然而,Implementation1Implementation2是多变的。这两个类共享很多相似的代码,因此将共享代码放在一个公共(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而不是 selffoo ,但问题是 this类型为 ImplementationBase<class Thing1, class Implementation1> ,但是Implementation1是必须的。显然,整个事情一团糟,ImplementationThing类耦合得太紧密,但如果不能重构 Thing,我看不到一个简单的出路。类。所以,最后,我的问题是:

  1. 有没有比使用 self 更好的替代方法?上面的技巧?
  2. 有没有一种设计可以更好地解决这个问题? (我有一种感觉,有,但我遗漏了一些明显的东西)

如果您已经读到这里,非常感谢您花时间阅读整个故事,对于这个冗长的问题,我再次表示歉意。

最佳答案

您已经在使用 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/

相关文章:

c++ - 使用右值引用重载模板化可变参数运算符

c# - 错误处理的设计模式

c++ - 警告 C++11 继承的构造函数

c++ - 使用 C++ 的递归线程使资源暂时不可用

c++ - 动态数组与 std::vector

python - 如何使用 SWIG 为 C++ 模板类创建调度包装类

c++ - 让函数在不指定模板参数的情况下获取指向模板对象的指针

java - 如果在桥接模式中,Abstraction 有一个 AutoClosable 实现者怎么办

.net - 空对象模式值得吗?

c++ - 指向二维矩阵语法的指针