The Fourth SOLID Principle: Interface Segregation in Java

Daniel Angel
5 min readSep 8, 2023

--

Photo by NEOM on Unsplash

The Fourth SOLID Principle, known as the Interface Segregation Principle (ISP), is a fundamental concept in object-oriented programming in Java. Simply put, ISP suggests that interfaces should be tailored to the specific needs of the classes that implement them. This means creating small, focused interfaces rather than large, generic ones. In this article, we will explore the Interface Segregation Principle in the context of Java and how to apply it effectively to build more maintainable and flexible software.

Tailored Interfaces

Imagine you are designing a document management system in Java. To represent documents and their actions, you might create an interface called Document containing methods for opening, saving, printing, and sending via email. But what happens when some types of documents don’t need all of these functions? That’s where ISP comes into play.

The Problem of Not Applying ISP

Let’s assume we have an interface called Document like the following:

public interface Document {
void open();
void save();
void print();
void sendViaEmail();
}

Then, we have two classes that implement this interface, PDFDocument and WordDocument:

public class PDFDocument implements Document {
public void open() {
// Logic for opening a PDF document
}

public void save() {
// Logic for saving a PDF document
}

public void print() {
// Logic for printing a PDF document
}

public void sendViaEmail() {
// Logic for sending a PDF document via email
}
}
public class WordDocument implements Document {
public void open() {
// Logic for opening a Word document
}

public void save() {
// Logic for saving a Word document
}

public void print() {
// Logic for printing a Word document
}

public void sendViaEmail() {
// Logic for sending a Word document via email
}
}

The problem here is that both PDFDocument and WordDocument are forced to implement all the methods of the `Document` interface, even if some of them are not relevant for those types of documents. This can lead to unnecessary and confusing code.

Applying ISP in Java

To apply the Interface Segregation Principle in Java, we need to split the Document interface into smaller, more specific interfaces. This allows classes to implement only what they need. Let’s see how we can do it:

public interface OpenSave {
void open();
void save();
}
public interface Print {
void print();
}
public interface SendViaEmail {
void sendViaEmail();
}

Now, classes can implement only the interfaces that are relevant to them:

public class PDFDocument implements OpenSave, Print {
public void open() {
// Logic for opening a PDF document
}

public void save() {
// Logic for saving a PDF document
}

public void print() {
// Logic for printing a PDF document
}
}
public class WordDocument implements OpenSave, SendViaEmail {
public void open() {
// Logic for opening a Word document
}

public void save() {
// Logic for saving a Word document
}

public void sendViaEmail() {
// Logic for sending a Word document via email
}
}

With this new structure, each class only implements the interfaces that they need, avoiding the need to implement irrelevant methods. This makes our code clearer and easier to maintain.

Drawbacks of ISP

While ISP is a valuable guideline for interface and class design, like any principle, it also has some drawbacks or limitations to consider:

1. Additional Complexity: Implementing ISP can lead to an increase in the number of interfaces in a system. This can make the design more complex, especially when working on large projects with many classes and relationships between interfaces.

2. Maintenance Overhead: If a change is made to an interface, it may require updating all the classes that implement it. This can increase the maintenance burden, especially in systems with many classes and a high dependency on interfaces.

3. Possible Excessive Small Interfaces: If ISP is followed too strictly, it could result in a large number of small, specific interfaces. This can make it harder to understand the system, as one would need to follow a large number of interfaces to grasp the relationships between classes.

4. Initial Design Challenge: Applying ISP from the start may result in premature over-design. At the beginning of a project, not all future needs and requirements may be known, potentially leading to an excessive division of interfaces.

5. Potential Conflict with Other Principles: In some cases, strictly following ISP may conflict with other SOLID principles like the Single Responsibility Principle (SRP) or the Dependency Inversion Principle (DIP). In these cases, a balance must be struck in applying different principles based on the project’s needs.

6. Increased Implementation Complexity: In small, simple projects, strictly applying ISP may seem excessive and increase implementation complexity without a clear benefit in terms of maintenance or flexibility.

The Combination of ISP and DIP

When you apply both ISP and DIP together, you achieve a more robust and flexible code design. Let’s see how they work together:

ISP establishes cohesive interfaces: With ISP, interfaces are designed to be specific and contain only related methods. This ensures that the classes implementing them only need to worry about implementing what they actually need.

DIP establishes abstract dependencies: With DIP, high-level classes depend on abstractions rather than concrete implementations. This means that high-level classes are not directly coupled to low-level classes. You can change the implementations without affecting the high-level classes.

Together, ISP and DIP reduce coupling in your code, which means that different parts of the system are not tightly interconnected. This makes your code easier to maintain, test, and extend as it evolves with changing requirements.

In summary, while ISP is a valuable principle for interface design, it should not be applied blindly. Instead of following it dogmatically, it should be carefully evaluated for application based on the specific needs of the project and how it may impact code comprehension, maintenance, and complexity. Striking a balance between small, cohesive interfaces and simplicity in design is important.

--

--

Daniel Angel
Daniel Angel

Written by Daniel Angel

Backend developer | Java, Spring boot, Cloud| Laravel, Php

No responses yet