Open Closed Principle in SOLID
SOLID
6 Articles
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
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.