Skip to content
Katabench
Try free

Strategy Over the Switch

Hard About 35 min Free, no account needed

A price calculator switches on a plan-name string and news up each concrete strategy by hand. Extracting an IPricingStrategy looks like the fix, but a tempting in-class factory keeps the concretes wired into the calculator. Make the calculator depend only on the abstraction and let the composition root choose the plan.

The brief

You've inherited a pricing engine. Pricing.PriceCalculator.Price(plan, amount) decides a price by switching on a plan-name string and, in each branch, reaching for the concrete formula by hand: new FlatStrategy(), new PercentStrategy(), new TieredStrategy(). The calculator knows the name of every plan and the type of every formula. Add a plan and you reopen this class, add an arm to the switch, and weld in one more concrete. It is closed against the very change it exists to absorb.

The shapes are already there. FlatStrategy, PercentStrategy, and TieredStrategy each live in their own file. But they implement no shared contract, so the only thing that can use one is code that already knows its concrete name, which is exactly what the switch does.

The trap

The tempting move is to extract an IPricingStrategy interface, have each plan implement it, then, to keep the call site tidy, give PriceCalculator a little factory of its own: static IPricingStrategy For(string plan) => plan == "percent" ? new PercentStrategy() : new FlatStrategy() ..., and call For(plan).Apply(amount). Do exactly that and the interface exists, the calculator "uses a strategy", you can even seal it, so most of the rules go green. But two stay stubbornly red, and they're the ones that matter. That For factory still says new FlatStrategy() and new PercentStrategy() right inside the calculator. The branch didn't leave; it just changed shape from a switch into a factory method. The calculator still names every concrete it was supposed to forget.

Your job

Make the calculator depend on nothing but the abstraction. It should hold an IPricingStrategy, call Apply, and never name or construct a single concrete plan, not in a switch, not in a factory. Move the one decision that genuinely needs to know the plans, which strategy answers a given plan name, out to where wiring belongs: the composition root (Solution). The behavior must stay identical: same price for the same (plan, amount).

How it's graded

Your submission must (1) still pass the behavior tests (identical prices) and (2) satisfy every rule shown in the results, including the two pinning that PriceCalculator references neither FlatStrategy nor PercentStrategy, not through a switch and not through an in-class factory. Extracting the interface clears the easy rules and leaves those two failing; only a calculator programmed purely against IPricingStrategy, with the concrete plans selected by the composition root, turns them all green.

"We extracted an interface" is only half of Open/Closed. If the class that should be closed against new plans still constructs them by name, the dependency on the concrete didn't go away. It just moved into a factory and hid.

How it is graded

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

SOLID principles exercises →

What it practices

Strategy pattern
Swap an algorithm behind an interface instead of branching on a type code.
Open-closed principle
Add behavior by adding types, not by editing a central switch.

Do these first

Related katas