The Open-Closed Principle of the SOLID principles acronym states that software entities such as classes and methods should be open for extension but closed for modification which encourages the use of abstraction and ensures developers can add new functionality without altering the current codebase and minimizing the risk of breaking existing features.

The main purpose of the Open-Closed principle is to ensure that developers write code that does not require modification everytime a customer comes back and changes the request (as developers know this can happen many times). Following the OCP can help us achieve the goal of extending the class’ behaviour with the customers request without modifying it.

Abstraction

Abstraction is a coding technique which enables developers to hide internal parts of a class and only expose the necessary features. With this enabled developers can create flexible designs and classes that can inherit the abstract class and implement their own interpretation of the abstract method.

An example of implementing the Open-Closed principle could be a salary calculator for developers at a company.

OCP C# Implementation

So now we will begin implementing the OCP in C#, create a new console application…

--

--

No responses yet