Skip to content

The Interface Segregation Principle

Difficulty: Intermediate

Key concepts

  • interface segregation principle
  • fat interface
  • role interface
  • client-function matrix
  • implementing several interfaces
  • interface explosion
  • What the Interface Segregation Principle (ISP) says, and the two kinds of harm a fat interface does: to the classes that implement it, and to the code that uses it.
  • How to draw a client–function matrix to see which client uses which functions.
  • How to split a fat interface into role interfaces, one per kind of client, and let one C++ class implement several of them.
  • How to avoid the opposite mistake, one interface per function.

A lab monitors its rooms with networked devices. The monitoring program has two parts that talk to devices:

  1. FR-1 A dashboard shows each device’s temperature and, for devices that measure it, humidity.
  2. FR-2 A firmware updater installs a new firmware image on every device that is not already running it.

When the program was written, the lab had one kind of device, a climate board that measures both temperature and humidity and can be updated. One interface, Device, described everything a climate board can do.

Then the lab bought cheap temperature probes: no humidity sensor, and firmware fixed at the factory. The developer added a TempProbe class. The next morning the dashboard showed an error instead of readings, and the nightly firmware roll-out failed halfway.

The Interface Segregation Principle says, in our words: code that uses an interface should not be forced to depend on functions it does not call.

A client is any code that uses an interface: here, the dashboard and the updater. ISP is the “I” in SOLID.

A fat interface hurts in two directions:

Who is hurt How
Implementers (TempProbe) Must implement every function, including ones they cannot honour, so they refuse at run time (an LSP violation)
Clients (dashboard, updater) Depend on functions they never call: a change to the firmware functions recompiles the dashboard; a test double for the dashboard must implement firmware functions; the type Device& does not say what the client really needs

The fix is not “make interfaces small”. It is: shape each interface around one kind of client, so that what a client depends on is exactly what it uses.

List every function of the fat interface, and mark who uses it and who can provide it:

VerifiedCompiled and tested with GNU 11.4.0 and Clang 14.0.0 (C++20, 2026-12-01).

before.hpp
// Every feature of every device, in one interface. An interface here is a
// class with only pure virtual functions (`= 0`): it says what a device can
// be asked, and each device class supplies the code. Every class derived
// from Device must implement all five, whether the hardware has the
// feature or not. That is the problem this page is about.
class Device {
public:
// Virtual destructor: deleting a device through a Device* runs the right
// derived destructor.
virtual ~Device() = default;
virtual std::string id() const = 0;
virtual double read_celsius() const = 0;
virtual double read_humidity_percent() const = 0;
virtual std::string firmware_version() const = 0;
virtual void install_firmware(const FirmwareImage& image) = 0;
protected:
// Protected: only derived classes may construct or copy the Device part.
// So code holding two Device& cannot write `a = b;`, which would copy
// only the Device part of one object into the other (slicing). See the
// "Interfaces" part of the site's C++ idiom reference.
Device() = default;
Device(const Device&) = default;
Device& operator=(const Device&) = default;
};
// A temperature probe: no humidity sensor, firmware fixed at the factory.
// It must still implement all five functions. For the two it cannot honour,
// the only choice left is to throw, and firmware_version answers with a
// made-up "factory". A caller holding a Device has no way to know in
// advance which devices will throw.
// `final`: no class may derive from TempProbe. `override` asks the compiler
// to check that each function really replaces one declared in Device.
class TempProbe final : public Device {
public:
TempProbe(std::string id, double celsius);
std::string id() const override { return id_; }
double read_celsius() const override { return celsius_; }
double read_humidity_percent() const override; // throws std::logic_error
std::string firmware_version() const override { return "factory"; }
void install_firmware(const FirmwareImage& image) override; // throws std::logic_error
private:
std::string id_;
double celsius_;
};
// The dashboard and the updater each use part of Device: the dashboard
// reads id and the two readings; the updater uses id, firmware_version,
// and install_firmware. Both take pointers to the full Device, so both
// depend on all five functions. The pointers are not owned.
std::vector<std::string> dashboard_lines(const std::vector<const Device*>& devices);
int roll_out(const std::vector<Device*>& devices, const FirmwareImage& image);
Device function Temperature panel Humidity panel Updater Climate board can Probe can
id() ✓ ✓ ✓ ✓ ✓
read_celsius() ✓ ✓ ✓
read_humidity_percent() ✓ ✓ no
firmware_version() ✓ ✓ no
install_firmware() ✓ ✓ no

The matrix splits the dashboard into two clients, a temperature panel and a humidity panel, because FR-1 asks for humidity only from devices that measure it: the two panels are shown for different sets of devices, so they need different things.

Read the matrix by columns. Each client uses a group of functions, and the groups barely overlap (only id() is shared). The probe can provide one group only. Each group is a candidate interface, a role: “something with a temperature”, “something with a humidity”, “something that can be updated”.

Before, both clients and both devices depend on one five-function interface; after, each client depends on its own role, and each device implements only the roles it can play
Text description of the diagram

Two parts, before and after. Before: the dashboard and the updater each have a dashed arrow labelled “uses 3 of 5” to the Device interface, which has five functions. ClimateBoard and TempProbe each have a thick “implements” arrow to Device; TempProbe’s note says three of its functions throw or return a made-up value. After: there are three interfaces, Thermometer, Hygrometer, and Updatable. The temperature panel, the humidity panel, and the updater each have a dashed arrow labelled “uses all of” to one of them, in that order. ClimateBoard has thick “implements” arrows to all three interfaces; TempProbe has one, to Thermometer only. A flow arrow from the Before group to the After group shows the redesign.

Diagram source (Mermaid)
flowchart TB
subgraph Before
  BD["dashboard"] -.->|uses 3 of 5| BI["Device<br/>5 functions"]
  BU["updater"] -.->|uses 3 of 5| BI
  BB["ClimateBoard"] ==>|implements| BI
  BP["TempProbe<br/>3 functions throw or fake"] ==>|implements| BI
end
subgraph After
  AT["Thermometer"]
  AH["Hygrometer"]
  AU["Updatable"]
  TP["temperature panel"] -.->|uses all of| AT
  HP["humidity panel"] -.->|uses all of| AH
  UP["updater"] -.->|uses all of| AU
  AB["ClimateBoard"] ==>|implements| AT
  AB ==>|implements| AH
  AB ==>|implements| AU
  AP["TempProbe"] ==>|implements| AT
end
Before --> After

interface (abstract class) Thermometer

for the temperature panel

Member functions

  • virtual std::string id() const = 0;
  • virtual double read_celsius() const = 0;

interface (abstract class) Hygrometer

for the humidity panel

Member functions

  • virtual std::string id() const = 0;
  • virtual double read_humidity_percent() const = 0;

interface (abstract class) Updatable

for the firmware updater

Member functions

  • virtual std::string id() const = 0;
  • virtual std::string firmware_version() const = 0;
  • virtual void install_firmware(const FirmwareImage& image) = 0; throws std::invalid_argument for an empty image

class ClimateBoard final

implements Thermometer, Hygrometer, Updatable; not copyable

Data members

  • std::string id_;
  • double celsius_;
  • double humidity_percent_;
  • std::string version_; starts at "2.0"

Member functions

  • ClimateBoard(std::string id, double celsius, double humidity_percent); throws if id is empty
  • std::string id() const override; one override for all three roles
  • double read_celsius() const override;
  • double read_humidity_percent() const override;
  • std::string firmware_version() const override;
  • void install_firmware(const FirmwareImage& image) override;

class TempProbe final

implements Thermometer only; not copyable

Data members

  • std::string id_;
  • double celsius_;

Member functions

  • TempProbe(std::string id, double celsius); throws if id is empty
  • std::string id() const override;
  • double read_celsius() const override;
After: three role interfaces, a board that plays all three, and a probe that plays one

The outline leaves out what every interface on this site has: a public virtual destructor, so an object can be deleted through a pointer to the interface, and protected copy operations, so an interface cannot be copied on its own (slicing off the device). FirmwareImage is a plain struct with a version string and the image bytes.

The roles. Each interface has a comment saying which client it is for. That comment is the design: if a new function does not serve that client, it does not belong in that interface. firmware_version and install_firmware stay together because the same client always uses them together.

VerifiedCompiled and tested with GNU 11.4.0 and Clang 14.0.0 (C++20, 2026-12-01).

roles.hpp
// One interface per kind of client. Each holds what that client needs,
// and nothing else. Each has the same shape as Device: pure virtual
// functions (`= 0`), a virtual destructor, and protected constructor and
// copy operations, so code holding two role references cannot copy one into
// the other (the "Interfaces" part of the site's C++ idiom reference
// explains why).
// None of them holds data, so a class that inherits several of them gets
// no duplicated state.
// For the dashboard's temperature panel. id() is repeated in every role,
// because every client needs to name the device it is talking about.
class Thermometer {
public:
virtual ~Thermometer() = default;
virtual std::string id() const = 0;
virtual double read_celsius() const = 0;
protected:
Thermometer() = default;
Thermometer(const Thermometer&) = default;
Thermometer& operator=(const Thermometer&) = default;
};
// For the dashboard's humidity panel.
class Hygrometer {
public:
virtual ~Hygrometer() = default;
virtual std::string id() const = 0;
virtual double read_humidity_percent() const = 0;
protected:
Hygrometer() = default;
Hygrometer(const Hygrometer&) = default;
Hygrometer& operator=(const Hygrometer&) = default;
};
// For the firmware updater. The version and the install are used
// together, by the same client, so they stay in one interface.
class Updatable {
public:
virtual ~Updatable() = default;
virtual std::string id() const = 0;
virtual std::string firmware_version() const = 0;
// Throws std::invalid_argument for an empty image.
virtual void install_firmware(const FirmwareImage& image) = 0;
protected:
Updatable() = default;
Updatable(const Updatable&) = default;
Updatable& operator=(const Updatable&) = default;
};

The example keeps the three roles in one header so that they fit on one screen. In a larger program each role would get its own header, so that a change to Updatable recompiles only the files that include updatable.hpp. That is the compile-time half of ISP; keeping them together, as here, gives only the design half.

The devices. A C++ class can implement several interfaces by deriving from each of them. Because the interfaces hold no data, this is the safe kind of multiple inheritance: there is no shared state to duplicate and no “diamond” to resolve.

VerifiedCompiled and tested with GNU 11.4.0 and Clang 14.0.0 (C++20, 2026-12-01).

devices.hpp
// Simulated drivers: each object stands for one physical device, so
// copying is deleted (two objects must not drive one board).
// Every feature, so every role. One id() overrides all three.
// It inherits from three interfaces at once (multiple inheritance). So a
// ClimateBoard* converts to a Thermometer*, a Hygrometer*, or an
// Updatable*, and can be handed to any of the three clients.
class ClimateBoard final : public Thermometer, public Hygrometer, public Updatable {
public:
// Throws std::invalid_argument for an empty id.
ClimateBoard(std::string id, double celsius, double humidity_percent);
// `= delete`: copying a ClimateBoard does not compile.
ClimateBoard(const ClimateBoard&) = delete;
ClimateBoard& operator=(const ClimateBoard&) = delete;
std::string id() const override { return id_; }
double read_celsius() const override { return celsius_; }
double read_humidity_percent() const override { return humidity_percent_; }
std::string firmware_version() const override { return version_; }
// Throws std::invalid_argument if the image has no version or no bytes,
// and then keeps the old version. Otherwise records image.version.
void install_firmware(const FirmwareImage& image) override;
private:
std::string id_;
double celsius_;
double humidity_percent_;
std::string version_ = "2.0"; // every board starts on version 2.0
};
// Only a thermometer: nothing to refuse. It has no humidity or firmware
// functions at all, so a TempProbe* does not convert to Hygrometer* or
// Updatable*, and passing one to those clients is a compile error.
class TempProbe final : public Thermometer {
public:
// Throws std::invalid_argument for an empty id.
TempProbe(std::string id, double celsius);
TempProbe(const TempProbe&) = delete;
TempProbe& operator=(const TempProbe&) = delete;
std::string id() const override { return id_; }
double read_celsius() const override { return celsius_; }
private:
std::string id_;
double celsius_;
};

Look at id(). All three roles declare virtual std::string id() const, and ClimateBoard overrides it once. In C++, one function in the derived class overrides the matching virtual function in every base, so calling id() through a Thermometer&, a Hygrometer&, or an Updatable& reaches the same code.

The drivers delete their copy operations. Each object stands for one physical device; a copy would be a second object driving the same hardware. The clients therefore take non-owning pointers to devices that main owns, use them for one call, and keep nothing.

The clients. Each asks for its role and nothing else:

VerifiedCompiled and tested with GNU 11.4.0 and Clang 14.0.0 (C++20, 2026-12-01).

clients.hpp
// Each client asks for the role it uses. The pointers are not owned and
// must not be null; the devices must outlive the call.
std::vector<std::string> temperature_lines(const std::vector<const Thermometer*>& devices);
std::vector<std::string> humidity_lines(const std::vector<const Hygrometer*>& devices);
// Installs `image` on every device whose version differs, in order, and
// returns how many it updated. If an install throws, the devices before
// it keep the new version and the rest are not touched.
int roll_out(const std::vector<Updatable*>& devices, const FirmwareImage& image);

VerifiedCompiled and tested with GNU 11.4.0 and Clang 14.0.0 (C++20, 2026-12-01).

clients.cpp
// The same loop as before; only the parameter type changed. Every pointer
// in `devices` is an Updatable, so every device can really be updated.
int roll_out(const std::vector<Updatable*>& devices, const FirmwareImage& image) {
int updated = 0;
for (Updatable* device : devices) {
// Skip devices that already run this version.
if (device->firmware_version() != image.version) {
device->install_firmware(image); // may throw for a bad image
++updated; // reached only if the install did not throw
}
}
return updated;
}

roll_out is the same loop as before. What changed is its parameter type: a TempProbe* cannot be put in a std::vector<Updatable*>, so the failure that happened at night now happens in the compiler.

Choice Why What it costs
One interface per client, not per device Clients depend on what they use; devices implement what they can do Three interfaces instead of one
firmware_version and install_firmware together One client always uses both; splitting them would add a type and gain nothing None
id() repeated in each role Every client needs to name a device; each role is complete on its own Three declarations of one function; one override covers them
Devices are not copyable One object per physical device Clients hold non-owning pointers; main must keep the devices alive
Clients take std::vector of role pointers A client can be given exactly the devices that play its role The caller decides which devices go to which client (in main here)
Interfaces with no data Multiple inheritance of interfaces stays simple: no duplicated state None

VerifiedCompiled and tested with GNU 11.4.0 and Clang 14.0.0 (C++20, 2026-12-01).

main.cpp
// main owns both devices. The clients get plain pointers to them (&board
// is the address of board) and keep nothing after the call.
ClimateBoard board{"lab-1", 21.5, 40.0};
TempProbe probe{"probe-7", 19.0};
// Each client gets only the devices that can play its role.
// `{&board, &probe}` builds the std::vector<const Thermometer*> that
// temperature_lines takes; each pointer is converted to point at the
// device's Thermometer part.
for (const std::string& line : temperature_lines({&board, &probe})) {
std::cout << " " << line << '\n';
}
// Only the board has a humidity sensor, so only the board is passed.
for (const std::string& line : humidity_lines({&board})) {
std::cout << " " << line << '\n';
}
const int updated = roll_out({&board}, image); // roll_out({&probe}, ...) does not compile
std::cout << " updated " << updated << " device(s) to " << board.firmware_version() << '\n';
Expected output
before:
dashboard failed: probe-7: no humidity sensor
roll-out failed: probe-7: firmware is fixed
after:
lab-1 21.5 C
probe-7 19.0 C
lab-1 40.0 %
updated 1 device(s) to 2.1

Before, both clients failed on the probe. After, the probe appears in the temperature panel only, and the board appears in all three. The comment in main names the call that would pass a probe to the updater; it does not compile (we checked by hand; no build test enforces it).

VerifiedCompiled and tested with GNU 11.4.0 and Clang 14.0.0 (C++20, 2026-12-01).

test_devices.cpp
// LLD_TEST(name) defines one test. LLD_CHECK(condition) records a failure
// if the condition is false, and LLD_CHECK_THROWS(expression, type) fails
// unless the expression throws that type of exception. They come from the
// site's small test header, lld_test.hpp.
// The flaw, pinned: one fat interface, so each client trips over a
// function the probe cannot honour. Both calls compile; both throw.
LLD_TEST(before_a_probe_breaks_both_clients) {
before::ClimateBoard board{"lab-1", 21.5, 40.0};
before::TempProbe probe{"probe-7", 19.0};
LLD_CHECK_THROWS(before::dashboard_lines({&board, &probe}), std::logic_error);
LLD_CHECK_THROWS(before::roll_out({&board, &probe}, image), std::logic_error);
}
// A test double for the temperature panel needs two functions, not five.
// It returns fixed values, so the test knows exactly what line to expect.
// namespace { ... } with no name keeps the class private to this file.
namespace {
class FakeThermometer final : public Thermometer {
public:
std::string id() const override { return "fake"; }
double read_celsius() const override { return -4.0; }
};
} // namespace
// The panel formats the fake's id and reading with one decimal place.
LLD_TEST(the_temperature_panel_needs_only_a_thermometer) {
const FakeThermometer fake;
const std::vector<std::string> lines = temperature_lines({&fake});
LLD_CHECK(lines.size() == 1);
LLD_CHECK(lines[0] == "fake -4.0 C");
}
// Roles are checked at compile time: a probe is only a thermometer.
// static_assert(condition) stops the build if the condition is false.
// std::is_convertible_v<From, To> is true when a From converts implicitly
// to a To; for pointers, that means "is a From one of the To kind?". The
// last line checks that the copy constructor is really deleted.
static_assert(std::is_convertible_v<TempProbe*, Thermometer*>);
static_assert(!std::is_convertible_v<TempProbe*, Updatable*>);
static_assert(!std::is_convertible_v<TempProbe*, Hygrometer*>);
static_assert(std::is_convertible_v<ClimateBoard*, Updatable*>);
static_assert(!std::is_copy_constructible_v<ClimateBoard>);
// One object, seen through three role references. All three reach the
// same board: the same id, and an update through Updatable& changes the
// board itself. The second roll_out finds version 2.1 already there.
LLD_TEST(one_board_plays_every_role) {
ClimateBoard board{"lab-1", 21.5, 40.0};
const Thermometer& thermometer = board;
const Hygrometer& hygrometer = board;
Updatable& updatable = board;
LLD_CHECK(thermometer.id() == "lab-1" && hygrometer.id() == "lab-1");
LLD_CHECK(roll_out({&updatable}, image) == 1);
LLD_CHECK(board.firmware_version() == "2.1");
LLD_CHECK(roll_out({&updatable}, image) == 0); // already up to date
}
// An image with no bytes: install_firmware throws before the version is
// recorded, so the board stays on 2.0 and roll_out never counts it.
// A probe with an empty id is refused by its constructor.
LLD_TEST(an_empty_image_is_refused) {
ClimateBoard board{"lab-1", 21.5, 40.0};
LLD_CHECK_THROWS(roll_out({&board}, FirmwareImage{"2.2", {}}), std::invalid_argument);
LLD_CHECK(board.firmware_version() == "2.0");
LLD_CHECK_THROWS(TempProbe("", 0.0), std::invalid_argument);
}

The first test pins the old failure. The second shows the quieter benefit: a fake for the temperature panel is two short functions. The static_assert lines check the roles at compile time: a probe is a Thermometer and nothing else, and a board is not copyable.

  • The drivers simulate hardware: readings are fixed numbers, and “installing” firmware only records the version. A real driver talks to a bus or a socket, and a reading can fail.
  • The reading functions are const because reading does not change what the driver object represents. A real driver might cache or count reads, and would mark those members mutable or drop the const; a mutable counter also needs synchronisation if the driver is shared between threads.
  • Which device goes to which client is decided by hand in main. A program with many devices would keep a registry that knows, for each device, which roles it plays.
  • roll_out stops at the first failed install; devices before it keep the new version. A real updater would report each device’s result.

ISP tells you to split by client. Splitting by function goes too far:

interface (abstract class) Named

Member functions

  • virtual std::string id() const = 0;

interface (abstract class) TemperatureReader

Member functions

  • virtual double read_celsius() const = 0;

interface (abstract class) HumidityReader

Member functions

  • virtual double read_humidity_percent() const = 0;

interface (abstract class) VersionReporter

Member functions

  • virtual std::string firmware_version() const = 0;

interface (abstract class) FirmwareInstaller

Member functions

  • virtual void install_firmware(const FirmwareImage& image) = 0;
Too far: one interface per function, so the updater needs three interfaces for one job

Now every client needs several interfaces for one job. The updater needs Named, VersionReporter, and FirmwareInstaller from the same device, so it must take three parameters that must refer to the same object (and nothing checks that), or go back to a combined type. Five interfaces with one function each say nothing about who uses what.

A quick test: if every client that uses one interface also uses the other, from the same object, they are one role. Merge them. If some clients use only one of the two, keep them apart, and let the client that needs both take both (or define a combined role that derives from the two).

The principle is about dependencies, not only about virtual. The same question applies to:

  • Function parameters. A function that needs a timeout should take the timeout, not the whole Config object; then it depends on one value, and a test passes one value.

  • Headers. A header that declares everything for everyone makes each includer recompile when any part changes. Split headers by who includes them.

  • Templates and concepts. A template that only calls read_celsius() should require only that. A C++20 concept can say so:

    template <class T>
    concept HasCelsius = requires(const T& device) {
    { device.read_celsius() } -> std::convertible_to<double>;
    };
    template <HasCelsius Device>
    double fahrenheit(const Device& device) {
    return device.read_celsius() * 9.0 / 5.0 + 32.0;
    }

    A concept that demands the whole device interface is a fat interface checked at compile time. (This sketch is not part of the example; it was compiled separately with g++ and clang++.)

  • One client. If only one piece of code uses an interface, its functions are, by definition, what that client uses.
  • Every implementer can honour every function, and the clients use most of it. A wide interface that everyone implements fully and every client uses is not “fat”; it is one cohesive abstraction. A Stack interface with push, pop, top, empty, and size is an example: every stack can do all five, and a client that pushes usually also pops.
  • The interface is not yours. A third-party or operating-system interface is what it is. Wrap it, and give your clients small roles on your side of the wrapper.
You gain You pay
Implementers provide only what they can do; no refusing overrides More interfaces, and classes that implement several
Clients are edited and retested only when their own role changes Someone (here main) must give each client the right devices
Test doubles are small A function that needs two roles must take both, or a combined role
The parameter type documents what a client really uses Role names must be chosen well, or the design becomes a list of fragments
  • Splitting by function instead of by client. See the outline above.
  • Splitting by what the code “is” rather than who uses it. The “better” design (sensing versus firmware) was tidy and still forced the probe to refuse.
  • Keeping the fat interface “for convenience” and adding roles beside it. Clients keep asking for the convenient fat one, and nothing improves. Remove it, or keep it only as a combination of roles that some client really needs.
  • Using dynamic_cast to discover roles. “If this Device* is also an Updatable*, update it” works, but hides from the type what the client needs. Prefer giving the client the right list.
  • Capability flags. bool has_humidity() on a fat interface is the run-time version of what a role interface says at compile time.

A new board also measures air pressure, and the dashboard gets a pressure panel. Add a Barometer role for the new panel; the new board implements it. The temperature panel, the humidity panel, the updater, and TempProbe need no edits (and, with one header per role, do not recompile).

The updater must reboot a device after installing. Add virtual void reboot() = 0; to Updatable, which only the updater uses and only updatable devices implement. The dashboard code needs no edits (and, with one header per role, does not recompile). (With the fat Device, the probe would have needed a fourth refusing function.)

Wireless probes run on batteries, and the dashboard should warn when a battery is low. A new client need, so a new role: BatteryPowered (id, battery_percent). Wireless probes implement Thermometer and BatteryPowered; the wired probe and the board do not change.

Exercise: Segregate an interface for two clients

A music app has one interface for its player:

interface (abstract class) MediaPlayer

Member functions

  • virtual void play() = 0;
  • virtual void pause() = 0;
  • virtual void set_volume(int percent) = 0;
  • virtual std::string current_title() const = 0;
  • virtual std::vector<std::string> playlist() const = 0;
  • virtual void download_for_offline(std::string_view title) = 0;
  • virtual void export_listening_stats(std::ostream& out) const = 0;
The exercise's starting point: one interface for everything the player can do

Two clients use it:

  • Car mode, a large-button screen for driving: play, pause, volume, and the current title.
  • Offline sync, a background job: reads the playlist and downloads each song for offline listening.

A third implementation is planned: a web player that streams only and cannot store songs.

  1. Draw the client–function matrix.
  2. Design role interfaces. Which functions go where?
  3. Which function is used by neither client, and what do you do with it?
  4. Which implementations play which roles?
Show the explained answer

1. Car mode uses play, pause, set_volume, and current_title. Offline sync uses playlist and download_for_offline. Neither uses export_listening_stats. No function is used by both.

2. Two roles, one per client:

interface (abstract class) PlaybackControls

for car mode

Member functions

  • virtual void play() = 0;
  • virtual void pause() = 0;
  • virtual void set_volume(int percent) = 0;
  • virtual std::string current_title() const = 0;

interface (abstract class) OfflineLibrary

for offline sync

Member functions

  • virtual std::vector<std::string> playlist() const = 0;
  • virtual void download_for_offline(std::string_view title) = 0;
One possible answer: a role for each client

Not Play, Pause, and Volume as separate interfaces: car mode always needs all four from the same player, so they are one role.

3. export_listening_stats has no client in this list. Do not put it in either role “to have it somewhere”. Either a client exists that the question did not mention (a settings screen, say), and it gets its own role, or nobody uses it, and it can be removed (YAGNI).

4. The app’s own player implements PlaybackControls and OfflineLibrary. The web player implements PlaybackControls only, so it cannot be handed to offline sync, and it never has to refuse download_for_offline. If the playlist is also needed by a screen in the web player, that screen’s need is a new question. The playlist is then used by two clients, so it becomes its own small PlaylistView role. Offline sync still needs the playlist and the download from the same player, so OfflineLibrary can derive from PlaylistView and add download_for_offline; the web player implements PlaylistView and PlaybackControls.

  • ISP: no client should be forced to depend on functions it does not use.
  • A fat interface hurts implementers (they refuse what they cannot do) and clients (they recompile, retest, and fake functions they never call).
  • Draw the client–function matrix; each client’s group of functions is a candidate role interface.
  • One C++ class can implement several roles. With no data in the interfaces, this is simple, and one override serves every role that declares the function.
  • Split by client, not by function: if a client always needs two interfaces from the same object, they are one.
  • The same idea applies to parameters, headers, and concepts: depend on what you use.