Grokking SOLID Design Principles
Ask Author
Back to course home

0% completed

Vote For New Content
Introduction to the Interface Segregation Principle
Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

The Interface Segregation Principle (ISP) is the fourth principle in the SOLID design principles. It states:

"Clients should not be forced to depend on interfaces they do not use."

This means that a class should only implement the methods it actually needs. Large, general-purpose interfaces can become problematic when they force implementing classes to include methods they don't require. Instead, it's better to create smaller, more focused interfaces.

The Problem: A Bloated Printer Interface

Let’s explore this principle using a real-world example: a printer system.

Imagine you’re tasked with designing a system for different types of printers. You decide to create a single interface for all printer-related functions. It might look like this:

Python3
Python3
. . . .

At first glance, this seems fine. The Printer interface has methods for printing, scanning, faxing, and stapling documents. However, what happens when you need to implement a simple BasicPrinter that only prints and doesn’t support scanning, faxing, or stapling?

Step 1: Implementing BasicPrinter

Let’s try to implement a basic printer that only prints documents.

Image
Python3
Python3
. . . .

As you can see, we’re already running into problems. The BasicPrinter class is forced to implement methods it doesn’t need, such as scanDocument(), faxDocument(), and stapleDocument(). Since it can’t perform these actions, we end up throwing exceptions. This bloats the code and creates unnecessary complexity.

Step 2: Implementing AdvancedPrinter

Now, let’s say we need to implement an AdvancedPrinter that supports all the functionalities (printing, scanning, faxing, and stapling).

Image
Python3
Python3
. . . .

The AdvancedPrinter works fine because it supports all the methods in the interface. But notice how both BasicPrinter and AdvancedPrinter had to implement the same interface, even though the BasicPrinter doesn’t use most of the methods.

Why This is a Problem

The problem here is that the Printer interface is too large. It forces classes that don’t need certain methods (like scanDocument() or faxDocument()) to implement them anyway. This leads to unnecessary code, and we’re left with methods that either do nothing or throw exceptions.

In other words, the classes are depending on methods they don’t use, violating the Interface Segregation Principle. The BasicPrinter should not be forced to implement scanning, faxing, or stapling, but because of the large interface, it has no choice.

.....

.....

.....

Like the course? Get enrolled and start learning!

Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible