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 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.
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.
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.
What it practices
- Interface segregation
- Split fat interfaces so clients depend only on what they use.
Related katas
- Make the Membership Contract Honest Hard Free An abstract Membership promises renewal even though lifetime memberships cannot renew, forcing RenewalService to recognize a concrete subtype before it can safely use the base contract. Split shared membership identity from the renewable capability, remove the lying inheritance, and make the service depend only on what it can honestly call.
- Split the God Port Hard Pro One fat warehouse interface serves a consumer that only reads and a consumer that also reserves, so reporting can see a method it must never call. Splitting the interface is not enough: each half needs its own published namespace, and the behavior tests force one implementation behind both.
- History Keeps a Checkpoint, Not Editor Internals Medium Free Capture immutable mementos that can be restored repeatedly while the history caretaker stays unaware of the editor’s stored representation.
- Four Layers for One Formula Medium Free A report-card service reaches its one weighted formula through a factory, a calculator contract, a base class, and a selector, each added so it could be swapped one day. Take out every layer nothing varies behind and keep the one abstraction that really varies, without changing a single report card.