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

后端 未结 14 1085
死守一世寂寞
死守一世寂寞 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 08:59

    I just want to emphasize that "Open/Closed", even though being obviously useful in OO programming, is a healthy method to use in all aspects of development. For instance, in my own experience it's a great painkiller to use "Open/Closed" as much as possible when working with plain C.

    /Robert

    0 讨论(0)
  • 2020-12-02 09:00

    In Design principle, SOLID – the "O" in "SOLID" stands for the open/closed principle.

    Open Closed principle is a design principle which says that a class, modules and functions should be open for extension but closed for modification.

    This principle states that the design and writing of the code should be done in a way that new functionality should be added with minimum changes in the existing code (tested code). The design should be done in a way to allow the adding of new functionality as new classes, keeping as much as possible existing code unchanged.

    Benefit of Open Closed Design Principle:

    1. Application will be more robust because we are not changing already tested class.
    2. Flexible because we can easily accommodate new requirements.
    3. Easy to test and less error prone.

    My blog post on this:

    http://javaexplorer03.blogspot.in/2016/12/open-closed-design-principle.html

    0 讨论(0)
  • 2020-12-02 09:00

    There are two versions of the Open Closed Principle.

    See the following for a clear and thorough explanation of both: https://www.slideshare.net/pjschwarz/first-expedia-tech-know-how-event-the-openclosed-principle-the-original-version-and-the-contemporary-version

    To whet your appetite, here are screenshots of the first few slides:

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-02 09:04

    The principle means that it should easy to add new functionality without having to change existing, stable, and tested functionality, saving both time and money.

    Often, polymorhism, for instance using interfaces, is a good tool for achieving this.

    0 讨论(0)
  • 2020-12-02 09:06

    I was recently given an additional idea of what this principle entails: that the Open-Closed Principle describes at once a way of writing code, as well as an end-result of writing code in a resilient way.

    I like to think of Open/Closed split up in two closely-related parts:

    • Code that is Open to change can either change its behavior to correctly handle its inputs, or requires minimum modification to provide for new usage scenarios.
    • Code that is Closed for modification does not require much if any human intervention to handle new usage scenarios. The need simply does not exist.

    Thus, code that exhibits Open/Closed behavior (or, if you prefer, fulfills the Open/Closed Principle) requires minimal or no modification in response to usage scenarios beyond what it was originally built for.

    As far as implementation is concerned? I find that the commonly-stated interpretation, "Open/Closed refers to code being polymorphic!" to be at best an incomplete statement. Polymorphism in code is one tool to achieve this sort of behavior; Inheritance, Implementation...really, every object-oriented design principle is necessary to write code that is resilient in the way implied by this principle.

    0 讨论(0)
提交回复
热议问题