Skip to content
Tutorials Center
My learning

Topics

Lesson 8 of 57 ยท Design Principles

Interface Segregation Principle

Learn the Interface Segregation Principle: why fat interfaces hurt, how role interfaces fix them, with a printer and scanner example and mixins.

The Interface Segregation Principle (ISP), the "I" in SOLID, says that no class should be forced to depend on methods it does not use. In practice it means: prefer several small, focused interfaces over one large, general-purpose one. Each client then sees only the part of an object it actually needs.

The problem it solves is easy to recognise. Someone designs a Machine interface with print, scan and fax, because the first product did all three. Then a cheap printer arrives that can only print, and the team has to implement scan and fax anyway, usually by throwing "not supported". That is a Liskov violation caused by a bloated interface. ISP attacks the cause rather than the symptom.

Fat interface, thin reality

One big contract print, scan, fax, staple, email.
Simple device Only prints, but must implement everything.
Stub methods Throws or does nothing for the rest.

Every change to the big contract touches every implementer.

1 / 2

What goes wrong with a fat interface

A fat interface causes three kinds of pain.

  1. Forced stubs. Implementers must write methods that make no sense for them. Those stubs either throw, which breaks callers, or silently do nothing, which hides bugs.
  2. Needless coupling. A client that only prints still depends on scan and fax. If the signature of fax changes, the print-only client is recompiled and retested for no reason.
  3. Hard testing. To fake a Machine in a test you must stub every method, even though the code under test calls just one.

ISP is closely related to the Single Responsibility Principle: a fat interface usually means a class or abstraction with several reasons to change. The difference is the viewpoint. SRP looks at the implementer; ISP looks at the client and asks what the client needs to know.

The printer and scanner example

Here is the fat design in JavaScript. JavaScript has no interface keyword, so we simulate the abstract base class by throwing in the defaults. The BasicPrinter can only print, but it inherits a contract that says it can do everything.

JS A fat interface in action

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Nothing stops digitize from receiving a BasicPrinter; the failure appears only at run time, in the middle of a feature. In a statically typed language the compiler would at least force BasicPrinter to write the stubs, which is arguably worse because the code compiles and then fails.

Splitting into role interfaces

A role interface describes one capability from the point of view of a client. digitize needs "something that can scan", so it should ask for exactly that. Concrete devices can then implement as many roles as they really have.

In Java this is natural:

Java
interface Printer { void print(String doc); }
interface Scanner { void scan(String doc); }
interface FaxSender { void fax(String doc); }

class BasicPrinter implements Printer {
    public void print(String doc) { /* ... */ }
}

class OfficeMachine implements Printer, Scanner, FaxSender {
    public void print(String doc) { /* ... */ }
    public void scan(String doc)  { /* ... */ }
    public void fax(String doc)   { /* ... */ }
}

A method void digitize(Scanner s, String doc) now refuses a BasicPrinter at compile time. The error moves from production to the editor.

C++ has no interface keyword either; the idiom is an abstract class whose methods are all pure virtual. A class may inherit from several such classes:

C++
struct Printer {
    virtual ~Printer() = default;
    virtual void print(const std::string& doc) = 0;
};

struct Scanner {
    virtual ~Scanner() = default;
    virtual void scan(const std::string& doc) = 0;
};

class OfficeMachine : public Printer, public Scanner {
public:
    void print(const std::string& doc) override { /* ... */ }
    void scan(const std::string& doc) override { /* ... */ }
};

Python uses duck typing, but since Python 3.8 you can describe a role with typing.Protocol. A type checker such as mypy then verifies that an argument has the right methods, with no inheritance needed:

Python
from typing import Protocol

class Scanner(Protocol):
    def scan(self, doc: str) -> str: ...

def digitize(device: Scanner, doc: str) -> str:
    return device.scan(doc)

These snippets are read-only: the site's runner cannot show compile-time errors for your own Java and C++ type design here, so treat the examples as real code to try on your own machine. Python checks Protocol only with an external type checker, not at run time.

Role interfaces with mixins in JavaScript

JavaScript offers a different way to get the same effect: mixins. A mixin is a function that takes a class and returns a new class with extra behavior. Each mixin is one role, and a device is built by stacking only the roles it has.

JS Roles as mixins

Press Run (or Ctrl + Enter) to see the console output here.

No output.

BasicPrinter has no scan method at all, so there is nothing to stub and nothing that can throw "not supported". The hasRole helper is a stand-in for what a compiler does in Java or C++: it asks whether the capability exists before the call. In production JavaScript you would typically document the expected shape, or use TypeScript interfaces, which give you the compile-time check.

Do not over-segregate

ISP is a guideline about clients, not a rule that every interface must have one method. An interface with read, write and close is perfectly fine if the clients that use it need all three together. Splitting it into three single-method interfaces would only add noise.

A practical way to decide: look at who calls the interface. If different clients use different subsets of the methods, split along those lines. If every client uses everything, leave it alone. Interfaces are shaped by their callers, not by their implementers.

A related technique appears in patterns later in this tutorial: the Adapter and the Facade both give a client a narrower view of something larger, which is ISP applied to existing code.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does the Interface Segregation Principle say?

    ISP asks for small, focused interfaces so that each client depends only on the capabilities it really needs.
  2. 2. A class implements a big interface and throws 'not supported' for half of the methods. What is the most likely cause?

    Forced stubs are the typical sign of a fat interface. Splitting it into role interfaces removes the need for them.
  3. 3. In the mixin example, why does BasicPrinter not need to stub scan()?

    Mixins add one role each. BasicPrinter applies only CanPrint, so it has no scan method to fake.
  4. 4. When is it reasonable to keep a multi-method interface as it is?

    Interfaces are shaped by their clients. If every client needs every method, splitting adds complexity without benefit.

Summary

  • ISP: no client should depend on methods it does not use.
  • Fat interfaces cause forced stubs, needless coupling and clumsy tests.
  • Split by client need into small role interfaces; a class implements the roles it truly has.
  • Java uses several interface types, C++ uses pure virtual base classes, Python can use Protocol, and JavaScript can stack mixins.
  • Do not split for its own sake: look at how callers use the interface.

Next lesson: Dependency Inversion and Dependency Injection.