AlgoViz

Interface segregation (ISP)

medium 50 min

No client should be forced to depend on methods it does not use.

Video lesson · Chapter 1 of 6

Make a prediction

0:00 / 4:23

See it run

Who sees which methods, who must lie, and how far a change travels.

Operations

A printer that must scan

The interface says that every device can do six things. This one can do one.

Step 1 of 5

Six methods promised

mail roomneeds 6 methodsresultarchiveneeds 6 methodsresultdeviceSimplePrintercanprintstubs5

Both clients accept any MultiFunctionDevice.

Step 1: Six methods promised

class SimplePrinter(MultiFunctionDevice):    def print(self, doc): ...    def scan(self): raise NotImplementedErrordef archive(device: MultiFunctionDevice):    return device.scan()def archive_fixed(device: Scannable):    return device.scan()
Use it to
Show a lying type
Watch out for
Five stubs
Error found
run time

Ask where the error is found: by the checker, or by the customer.

In the real world

The same interface segregation doing real jobs. Pick one and step through it.

One remote with forty buttons, or one per need.

Step 1 of 4

Forty buttons

ashawants lightneeds2 buttonsremotein the handbuttons40never used38lampLamplightoffgarageGaragedoorclosed

Asha needs two of them.

Step 1: Forty buttons

Common mistakes

The bugs interviewers watch for, with the fix beside each.

  • A copy of the class as interface

    major

    All callers are as coupled as before. There is only one file more.

    Wrong
    class IOrderStore(ABC):   # all 31 methods
    Right
    class DayReader(Protocol):    def list_day(self, day): ...
  • Throwing in a stub

    critical

    The type says yes, the object says no, and the customer finds out.

    Wrong
    class Robot(Worker):    def eat(self): raise NotImplementedError
    Right
    class Robot(Workable, Reportable): ...
  • Casting back

    critical

    The narrow type is a lie if the caller reaches behind it.

    Wrong
    def report(orders: DayReader):    cast(OrderStore, orders).purge("...")
    Right
    def clean(orders: Retention, before):    orders.purge(before)
  • Tearing a pair

    major

    Two roles may come from two objects. Then the lock that was taken is not the one released.

    Wrong
    def use(a: Lockable, b: Unlockable): ...
    Right
    def use(lock: Lock):    with lock: ...
  • A wrapper that forgets

    major

    Optional abilities of the inner object vanish without an error.

    Wrong
    class Count:    def write(self, t): ...   # flush is gone
    Right
    def counting(inner):    return KeepsFlush(inner) if isinstance(inner, Flushable) else LosesFlush(inner)

Interview tips

  • Start at the callers

    • Rows: clients
    • Columns: methods
    • Same callers, same role
  • Name the need

    • Printable, Refunds
    • Not IOrderStore2
    • No name, wrong group
  • Know when to stop

    • Used together
    • Tied by a rule
    • One method: a function
  • Think of outsiders

    • New abstract method breaks
    • Default if computable
    • Second role if optional