I.25: Prefer abstract classes as interfaces to class hierarchies(在类体系中将抽象类定义为接口会更好)

译者注:

  1. 抽象类:不能直接生成示例的基类。
  2. 接口:不包含数据成员的抽象类。

Reason(原因)

Abstract classes are more likely to be stable than base classes with state.

抽象类有很大的可能比包含状态的基类更稳定。

译者注:结合全文,这里的抽象类应该指接口。

Example, bad(反面示例)

You just knew that ​​Shape​​ would turn up somewhere :-)

你只知道Shape会在某处表示:-)

 

class Shape {  // bad: interface class loaded with datapublic:    Point center() const { return c; }    virtual void draw() const;    virtual void rotate(int);    // ...private:    Point c;    vector<Point> outline;    Color col;};

This will force every derived class to compute a center -- even if that's non-trivial and the center is never used. Similarly, not every ​​Shape​​​ has a ​​Color​​​, and many ​​Shape​​​s are best represented without an outline defined as a sequence of ​​Point​​s. Abstract classes were invented to discourage users from writing such classes:

这将强迫每个派生类都要计算中心位置-哪怕是那些特殊且中心从未不用的图形。类似的,也不是所有的图形都有颜色,很多图形最好不要包含一个有Points序列定义的轮廓线。发明抽象类就是为了鼓励用户写出下面的类:

 

class Shape {    // better: Shape is a pure interfacepublic:    virtual Point center() const = 0;   // pure virtual functions    virtual void draw() const = 0;    virtual void rotate(int) = 0;    // ...    // ... no data members ...    // ...    virtual ~Shape() = default;};

 

Enforcement(实施建议)

(Simple) Warn if a pointer/reference to a class ​​C​​​ is assigned to a pointer/reference to a base of ​​C​​ and the base class contains data members.

(简单)如果指向类C的指针或引用被复制被C的基类类型的指针或引用,而且基类包含数据成员时,报警。

译者注:也就是说,大师的意见是:基类基本上抽象类就不要有数据成员了。

 


阅读更多更新文章,请关注微信公众【面向对象思考】