Open Closed Principle (OCP)

The Open-Closed Principle states that software entities (e.g., classes, modules, functions) should be open for extension but closed for modification. This means that the behavior of a module can be extended without altering its source code, helping maintain stability and avoid introducing bugs.

Key aspects of OCP

Example

Imagine a shape-drawing program. Initially, it supports only circles, and you need to add functionality for rectangles:

By adhering to OCP, developers create systems that are more flexible, maintainable, and easier to enhance without compromising the integrity of the existing codebase.