Skip to content
Katabench
Try free

Program to the Abstraction

Hard About 25 min Free, no account needed

An IPaymentProcessor abstraction exists, but CardProcessor only duck-types it and a second implementer, LegacyCharger, honours it under the wrong name. Wiring up the obvious processor clears the implements rule but leaves the role's naming contract red. Make every payment method a true, conventionally-named drop-in over a proper abstract base.

The brief

The codebase already declares the abstraction its callers expect, IPaymentProcessor, with a single Charge(decimal amount) method. There's also a proper shared base, an abstract PaymentMethod, that both payment methods extend for their common money-formatting; that part is already correct, so leave it abstract. The trouble is that nothing yet honours the interface as a real contract, and it's broken in two different ways at once.

CardProcessor extends PaymentMethod and happens to have a Charge method with a matching signature, but it does not implement IPaymentProcessor, so it is not a drop-in for anything typed to the interface. Extending the base is not the same as being substitutable behind the abstraction: a caller holding an IPaymentProcessor can't accept a CardProcessor.

LegacyCharger, on the other hand, does implement IPaymentProcessor (it really is substitutable behind the abstraction), but it is named LegacyCharger, not *Processor. The catalog pins a naming convention on the role: every implementer of IPaymentProcessor is a *Processor, precisely so that reaching for the abstraction yields a predictable family of types. LegacyCharger violates that.

The trap

The tempting move is to fix the one that's staring at you: declare class CardProcessor : IPaymentProcessor and call it done. Do exactly that and the "CardProcessor implements the abstraction" rule goes green. But one rule stays stubbornly red, and it's easy to miss: the naming convention. LegacyCharger is still an implementer of IPaymentProcessor, and it is still named Charger. Wiring up the obvious processor does nothing about the second implementer that breaks the role's contract. "I made the obvious one implement the interface" is only half the job, because programming to an abstraction means the whole family behind it is substitutable and predictable, not just the type you happened to look at first.

Your job

Make every payment method a true, conventionally-named drop-in. Have CardProcessor implement the abstraction (on top of the base it already extends), and bring LegacyCharger into line with the role's naming contract too: rename it so it ends in Processor. Keep the shared PaymentMethod base abstract (it's a contract to extend, never a type to new directly), and don't let it implement IPaymentProcessor itself, since only the concrete *Processor leaves belong to the family. The behavior must stay identical: same charge text for the same amount.

How it's graded

Your submission must (1) still pass the behavior tests (same charge text) and (2) satisfy every architecture rule shown in the results: that IPaymentProcessor exists, that CardProcessor implements it, that every implementer of the abstraction is named *Processor, and that the shared PaymentMethod base is abstract. The naive "just implement the obvious one" clears the implements rule and leaves the naming rule red; only bringing the whole family behind the abstraction into line turns them all green.

Having a method with the right name is not implementing the interface, and implementing the interface on one type is not "programming to the abstraction" either. The abstraction is only real when every type behind it is a substitutable, predictable member of the family.

How it is graded

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

SOLID principles exercises →

What it practices

Liskov substitution
Subtypes must honor every promise their base type makes.

Related katas