SOLID — From Zero To Potato 2

About Open/Closed Principle (OCP)

YE MON KYAW
Arpalar Tech
3 min readMar 29, 2023

--

This article is explaining about ‘SOLID’ in terms of example, if you have not read Part 1 yet or if you don’t know about what is Single-responsibility principle, please read the Part 1.

Today i will explain about Open/Closed Principle (OCP). In OCP class should be open for extension but closed for modification.This principle encourages us to write code in a way that allows for easy extension without modifying existing code.

In the context of a Student class,we should design the class in a way that allows us to add new features or behaviors to a student object without having to modify the existing class code. For example, we might use inheritance to create different types of students (such as online students or international students) that inherit from a base Student class.

Are you confusing about how will be in real time use case?, don’t worry you will be understand after reading the article.

Let’s say we have a Student class with a method that calculates the grade point average (GPA) of the student based on their grades:

class Student(val name: String, val grades: List<Double>) {
fun calculateGPA(): Double {
var sum = 0.0
for (grade in grades) {
sum += grade
}
return sum / grades.size
}
}

Now we want to add a new feature to the Student class that allows us to calculate the weighted GPA of the student based on the credits associated with each grade. One way to implement this feature is to modify the existing calculateGPA method to include the credits:

class Student(val name: String, val grades: List<Pair<Double, Int>>) {
fun calculateGPA(): Double {
var sum = 0.0
var creditSum = 0
for ((grade, credits) in grades) {
sum += grade * credits
creditSum += credits
}
return sum / creditSum
}
}

While this modification allows us to add the new feature of calculating weighted GPA, it violates the Open/Closed Principle, because we had to modify the existing class to add the new functionality. If we keep modifying the existing class every time we want to add a new feature, we will end up with a class that is too large and too complex.

So let find the better way in terms of OCP,so i refactor above class by using inheritance and create a new subclass that adds the new functionality:

open class Student(val name: String, val grades: List<Double>) {
open fun calculateGPA(): Double {
var sum = 0.0
for (grade in grades) {
sum += grade
}
return sum / grades.size
}
}

class WeightedGPAStudent(name: String, grades: List<Pair<Double, Int>>) : Student(name, grades) {
override fun calculateGPA(): Double {
var sum = 0.0
var creditSum = 0
for ((grade, credits) in grades) {
sum += grade * credits
creditSum += credits
}
return sum / creditSum
}
}

In this refactored version, we have created a new subclass WeightedGPAStudent that inherits from the base Student class. This allows us to add the new feature of calculating weighted GPA without modifying the existing class. We can continue to add new features by creating new subclasses that inherit from the base Student class, without modifying the existing code.

This approach adheres to the Open/Closed Principle because it allows us to extend the functionality of the Student class without modifying the existing code. The base class is closed for modification, but open for extension through inheritance.

Hope will be more understanding about Open/Closed Principle and this article will be helpful for you.

--

--

YE MON KYAW
Arpalar Tech

Software Engineer who write code in Kotlin / Android