⭐ Please help by giving this repository a star. It takes seconds and help thousands of developers! ⭐

I Love .NET Star Request

🤝🏿 Please share with the community! 🤝🏿

As an open-source project without funding, I cannot afford advertising I ❤️ .NET in a typical way. Instead, the project relies on community interactions. Please consider sharing a post about I ❤️ .NET and the value it provides. It really does help!

Open Closed Principle in SOLID

Open Closed Principle in SOLID

solid

6 Articles

Improve

In this article, let's learn about Open Closed Principle in SOLID Principles in .NET.

Note: If you have not done so already, I recommend you read the article on Single Responsibility Principle in SOLID.

Table of Contents

  1. Introduction
  2. When to apply OCP?
  3. Demo
  4. Advantages
  5. Summary

Introduction

The Open Closed Principle (OCP) is a fundamental principle of SOLID. The OCP states that software entities should be open to extension but closed to modification. We all are using OCP principle in DTO's / POCO's without even knowing that we are using it. In API's we send back response to client using DTO. Once clients starts to consume we cannot remove a property from DTO as it might break the clients which means closed for modification. But we add new property to DTO to send new data / information to clients which means open for extension.

When to apply OCP?

The Open Closed Principle should be applied to any software entity that is likely to change over time. This includes classes, methods, and interfaces. When designing software, it is essential to consider the possibility of future changes, and the OCP helps to future-proof our codebase.

Demo

Let's take a look at a C# code sample that demonstrates the Open Closed Principle. In this example, we have an interface called IShape that defines a method called Area. We then have two classes, Rectangle and Circle, that implement the IShape interface and provide their implementation of the Area method.

Code Sample - OCP - Open Closed Principle - Before

Now let's say we need to add a new shape to our system, a Triangle. We can do this without modifying the existing code by creating a new class that implements the IShape interface and provides its implementation of the Area method.

Code Sample - OCP - Open Closed Principle - After

By following the Open Closed Principle, we were able to extend our system to include a new shape without modifying the existing code. This allows us to avoid introducing bugs and breaking existing functionality.

Advantages

The Open Closed Principle has several advantages, including:

  • Testability: Makes code more testable by reducing the need for regression testing when making changes. This allows for faster and more efficient testing.
  • Maintainability: Makes code more maintainable by reducing the risk of introducing bugs and breaking existing functionality when making changes.
  • Reusability: By designing software entities to be open to extension but closed to modification, we can create more reusable code.
  • Scalability: By allowing software entities to be extended without modifying the existing code, the OCP makes code more scalable and adaptable to changing requirements.
  • Better Teamwork: Team members can work on different parts of the system simultaneously without interfering with each other's work. This leads to more efficient teamwork and a more efficient development process.

Summary

The Open Closed Principle is an essential principle of object-oriented design that states that software entities should be open to extension but closed to modification. By following this principle, we can ensure that our software is more maintainable, scalable, and adaptable to changing requirements. The use of abstraction, inheritance, and interfaces allows us to extend our software entities without modifying the existing code, reducing the risk of introducing bugs and breaking existing functionality. By following the OCP, we can create more flexible, maintainable, and robust codebases that are easier to test and scale.

  • Solid
  • OCP
  • Open Closed