Skip to content
Katabench
Try free

Compose the Fees

Hard About 35 min Free, no account needed

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 brief

Banking.Account charges a monthly fee. Premium accounts waive it above a balance threshold; student accounts never pay one. Someone modeled that variation the classic way: a virtual MonthlyFee on the base and a subclass per flavor. It works, and it is quietly rotting. The base class can never be sealed while subclasses hang off it. Every new flavor (a promo month, a regional rate, premium students) is another class in the hierarchy, and combining two flavors has no answer at all except yet another subclass that duplicates both. The account's identity (holding a balance, being charged) is fused to one axis of variation that wanted to be a replaceable part.

The trap

Half-composing is the subtle failure here. Suppose you extract IFeePolicy and the three concrete policies, delete the subclasses, and then, to keep Account convenient to construct, give it a default: private readonly IFeePolicy _fees = new StandardFeePolicy();. Almost every rule goes green. But Account now names a concrete policy, and the rule pinning that the account references no StandardFeePolicy stays red. Choosing the default flavor is wiring, and wiring belongs to the composition root. An account that quietly defaults to "standard" has kept a little switch statement of one case inside itself.

Your job

Turn the inheritance axis into a composed part. Declare IFeePolicy with the fee computation. Write StandardFeePolicy, PremiumFeePolicy, and StudentFeePolicy (the naming rule expects every implementer to end in Policy). Collapse the hierarchy: one sealed Account that takes its policy through the constructor, holds it readonly, and delegates the fee question. The Solution class picks the policy per account kind and passes it in. Same inputs, same charges, to the cent.

How it's graded

Your submission must (1) still pass the behavior tests (identical charges for every kind and balance) and (2) satisfy every rule shown in the results: the policy abstraction and all three concrete policies exist and are named like policies, Account is sealed, references IFeePolicy, keeps readonly fields, and names no concrete policy. Keeping any subclass leaves Account unsealable; hard-coding a default policy leaves the no-concrete rule red. Only full composition with outside wiring turns the board green.

Inheritance shares code by freezing a decision into the type graph at compile time. Composition shares code by making the decision a value you pass in. When the thing that varies is a policy, pass the policy.

How it is graded

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

SOLID principles exercises →

What it practices

Composition over inheritance
Combine small behaviors instead of multiplying subclasses or boolean flags.
Dependency injection
Ask for collaborators through constructors instead of newing them up or locating them.

Related katas