13th April, 2020 Output
Overview
The Open/Closed Principle(OCP) is one of five design principles for object-oriented software development.
In object-oriented programming, the open/closed principle states "software entities should be open for extension, but closed for modification"
A class is closed, since it may be compiled, stored in a library, baselined, and used by client classes. But it is also open, since any new class may use it as a parent, adding new features.
Goal of OCP
The goal is to make the system easy to extend without incurring a high impact of change. This goal is accomplished by partitioning the system into components and arranging those components into a dependency hierarchy that protects higher-level components from changes in lower-level components.
Ways to achieve OCP
Inheritance: Here, a new class may inherit from a parent class, then add new features. This approach introduces tight coupling if the subclasses depend on the implementation details of their parent class.
Polymorphic Open/Closed Principle: It uses interfaces instead of superclasses to allow different implementations which you can easily substitute without changing the code that uses them. The interfaces are closed for modifications, and you can provide new implementations to extend the functionality of your software. The main benefit of this approach is that an interface introduces an additional level of abstraction which enables loose coupling. The implementations of an interface are independent of each other and don’t need to share any code.
Reference
Robert C. Martin "Clean Architecture, A Craftsman’s Guide to Software Structure and Design"