我想获得关于以下三种设计模式的stackoverflow社区的意见。首先是实现继承;第二个是接口继承;第三个是中间地带。我的具体问题是:哪个最好?实现/接口继承设计问题
实现继承:
class Base {
X x() const = 0;
void UpdateX(A a) { y_ = g(a); }
Y y_;
}
class Derived: Base {
X x() const { return f(y_); }
}
接口继承:
class Base {
X x() const = 0;
void UpdateX(A a) = 0;
}
class Derived: Base {
X x() const { return x_; }
void UpdateX(A a) { x_ = f(g(a)); }
X x_;
}
中间地带:
class Base {
X x() const { return x_; }
void UpdateX(A a) = 0;
X x_;
}
class Derived: Base {
void UpdateX(A a) { x_ = f(g(a)); }
}
我知道很多人喜欢接口继承实现继承。然而,后者的优点是,通过指向Base
的指针,可以内联x(),并且可以静态计算x_
的地址。