SOLID — Open-Closed Principle (Part 2) _ by Matthias Schenk _ Towards Dev
SOLID — Open-Closed Principle (Part 2) _ by Matthias Schenk _ Towards Dev
In the last article I started my series about the SOLID principles. The first
part was dedicated to the Single Responsibility Principle. Today I want to
continue with the next principle, which stands behind the O of the acronym.
The advantages that OCP brings to your application are similar to the ones of
the Single Responsibility Principle but for different reasons.
https://towardsdev.com/solid-open-closed-principle-part-2-ed1bdc5a2326 1/7
12/21/24, 8:43 PM SOLID — Open-Closed Principle (Part 2) | by Matthias Schenk | Towards Dev
Example
To show the Open-Closed Principle in action, let’s consider an example of a
messaging application that supports multiple messaging services such as
SMS, Email, and Push Notifications.
The first version will be one that clearly violates the principle in order to
show the problems that will arise:
class MessagingClient {
fun sendNotification(service: String, message: Message) {
when (service) {
"SMS" -> {
// Send SMS
}
"Email" -> {
// Send Email
}
"PushNotification" -> {
// Send Push Notification
}
else -> {
throw IllegalArgumentException("Invalid service")
}
}
}
}
https://towardsdev.com/solid-open-closed-principle-part-2-ed1bdc5a2326 2/7
12/21/24, 8:43 PM SOLID — Open-Closed Principle (Part 2) | by Matthias Schenk | Towards Dev
I think it is clear now, why the above solution is not a good one, so I will
change the design of the system to make adding new messaging services,
without modifying the existing code, possible.
I start by creating an interface that defines the common behavior for all
messaging services. This abstraction acts as the contract that all concrete
messaging services must fulfill.
interface MessagingService {
fun sendMessage(message: Message)
}
https://towardsdev.com/solid-open-closed-principle-part-2-ed1bdc5a2326 3/7
12/21/24, 8:43 PM SOLID — Open-Closed Principle (Part 2) | by Matthias Schenk | Towards Dev
To decouple the code that uses the messaging services from the specific
implementations, I can use polymorphism and dependency injection. This
allows me to switch between different messaging services without modifying
the client code.
To add a new messaging service, I can simply create a new class that extends
MessagingService and implements the required methods. I can then inject the
new service into the MessagingClient without modifying the existing code:
Conclusion
The Open-Closed Principle is a fundamental principle in software
development that promotes code that is open for extension but closed for
modification. By designing software components that care about this
https://towardsdev.com/solid-open-closed-principle-part-2-ed1bdc5a2326 4/7
12/21/24, 8:43 PM SOLID — Open-Closed Principle (Part 2) | by Matthias Schenk | Towards Dev
In the next part of this series I will talk about the Liskov Substitution
Principle.
No responses yet
What
are
your
thoughts?
Respond
https://towardsdev.com/solid-open-closed-principle-part-2-ed1bdc5a2326 5/7