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.
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.
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.
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.
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
- Wrap It, Don't Flag It Hard Free An announcer dresses up messages with boolean flags, and every new dressing doubles the combination space inside one method. Rebuild it as stackable decorators over an IAnnouncer abstraction. Subclassing the plain announcer, or letting a decorator new up its own inner, fails the sealed and wraps-any rules.
- Null Object: No Branching Medium Free A cart guards a nullable discount with a null check at every use. Introduce a NoDiscount Null Object so the cart never branches on absence, but do not let the cart hard-code that default. The obvious 'default it inside the cart' move re-couples the cart to a concrete; the default belongs to the composition root.
- Retire the Service Locator Medium Free A quote calculator fetches its rate table from a global ServiceLocator, so its one real dependency is invisible in every signature. Replace the locator with constructor injection. Caching the located table into a readonly field keeps the calculator quietly coupled to the global and leaves the key rule red.
- Ask for the Interface Easy Free An order notifier news up its own SmtpEmailer and types the field as the concrete class, even though an IMessenger abstraction sits right next to it. Make the notifier ask for the interface in its constructor. Typing the field as IMessenger while still newing the concrete inside is not the fix.