Skip to content
Katabench
Try free

Shrink the Machine Interface

Medium About 20 min Free, no account needed

One fat IMachine interface forces the inkjet printer to fake a Scan it cannot perform, and the print queue to demand a scanner it never uses. Split the roles into IPrinter and IScanner. Keeping IMachine around as a composed sub-interface leaves the inkjet transitively welded to scanning and two rules red.

The brief

The office module has one interface for every machine: Office.IMachine, with Print and Scan. The copier is happy. The inkjet printer is not: it cannot scan, so its Scan method throws NotSupportedException and hopes nobody calls it. Meanwhile PrintQueue, which only ever prints, still demands a full IMachine, so every caller must supply a scanner it will never use. One fat contract has produced a liar and a hostage. This is the Interface Segregation Principle: no client should be forced to depend on members it does not use, and no implementer should be forced to fake members it cannot honor.

The trap

The tempting move is to keep IMachine alive as a convenience: declare IPrinter and IScanner, then write IMachine : IPrinter, IScanner and leave InkjetPrinter : IMachine in place. The two new interfaces exist, so the easy rules go green. But an implementer of a composed interface carries every parent it inherits, so the inkjet still ends up welded to IScanner and still references IMachine, and both of its rules stay red. A fat interface renamed into a bundle is still a fat interface.

Your job

Split the contract along its capabilities. Declare IPrinter and IScanner as separate role interfaces. InkjetPrinter implements only IPrinter and its throwing Scan disappears entirely. Copier implements both roles honestly. PrintQueue asks for exactly what it uses: an IPrinter. The observable output for every test stays the same.

How it's graded

Your submission must (1) still pass the behavior tests and (2) satisfy every rule shown in the results: the two role interfaces exist, the inkjet is a printer that references neither IScanner nor IMachine, the copier is a scanner, and the queue depends on IPrinter and not IMachine. The composed-interface shortcut clears the existence rules and leaves the inkjet and queue rules red; only a real split turns everything green.

An interface belongs to its clients, not to its implementers. When a class must throw to satisfy a contract, the contract is wrong: it is describing a category of machine that this machine is not.

How it is graded

Executable structural checks grade the shape of your types as well as their behavior.

SOLID principles exercises →

What it practices

Interface segregation
Split fat interfaces so clients depend only on what they use.

Related katas