What is the meaning and reasoning behind the Open/Closed Principle?

后端 未结 14 1187
死守一世寂寞
死守一世寂寞 2020-12-02 08:31

The Open/Closed Principle states that software entities (classes, modules, etc.) should be open for extension, but closed for modification. What does this mean, and why is i

14条回答
  •  旧巷少年郎
    2020-12-02 09:02

    An additional rule of thumb for conforming to OCP is to make base classes abstract with respect to functionality provided by derived classes. Or as Scott Meyers says 'Make Non-leaf classes abstract'.

    This means having unimplemented methods in the base class and only implement these methods in classes which themselves have no sub classes. Then the client of the base class cannot rely on a particular implementation in the base class since there is none.

提交回复
热议问题