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 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.
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).
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.
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
- The Core Doesn't Know Its Plugins Medium Pro A report host switches on format names and news up every plugin, and the plugins call back into the host and into each other for the report's currency rules. Let the composition root register plugins, refuse two plugins claiming one format, and hand each plugin the host's capabilities through a context the SDK publishes.
- A Rule Tree Can Contain Any Rule Hard Free Replace a central tagged-tree evaluator with recursive rule objects that preserve child order, empty identities, and short-circuit behavior.
- Compose the Fees Hard Free Account fee variation is modeled by subclassing: PremiumAccount and StudentAccount each override MonthlyFee, so the base can never be sealed and every new flavor is a new class. Flatten the hierarchy into one sealed Account composed with an injected IFeePolicy. Extracting the policy but hard-coding a default inside the account trips the composition-root rule.
- The Leaky Payment Port Hard Free A checkout service is welded to a Stripe client and passes vendor DTOs around. Someone half-extracted an IPaymentGateway port, but it leaks Stripe types through its own signature. Make the port vendor-neutral with a real anti-corruption layer, not an interface that smuggles the SDK through.