The SOLID Principles — OCP, LSP, ISP

KRISHNA KISHORE V
5 min readJun 15, 2023

In the previous article, we have seen Single Responsibility Principle from both the OOP and Microservices perspectives. In this article, we will look at the other principles.

The next one is the Open-Closed Principle.

Open-Closed Principle

This principle states that the modules should be open for extension and closed for modification.

For this, we luckily have the same example from the previous article. Remember the classes from the previous article which are pasted below.

class Patient {
private int id;
private String name;
// .....
// .....
// Getters & Setters
}

class PatientRepository {
public void saveToMySql(Patient patient) { ... }
public void saveToOracle(Patient patient) { ... }
public void saveToMongo(Patient patient) { ... }
// ...
}

class Exporter {
public void toPDF() { ... }
public void toWord() { ... }
public void toPNG() { ... }
// ...
}

We have already looked at this example in the previous article. In the above example, the classes PatientRepository and Exporter are clearly violating the OCP principle. Because, for PatientRepository, let’s say we need to change the database to Cassandra then the class needs to be modified with a new method such as saveToCassandra() which violates the OCP. Because OCP says the class should be closed for modifications. So the solution for this is to separate out and encapsulate the specific functionality…

--

--

KRISHNA KISHORE V

Full Stack Tech Lead | Software Consultant | Technical Content Writer