Wrap It, Don't Flag It
Hard About 30 min Free, no account needed
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.
The brief
Announcements.Announcer dresses up a message before it goes out: maybe shouted to uppercase,
maybe framed in a banner. Each dressing is a boolean parameter, and the method body is a queue of
if (flag) blocks. Two flags give four combinations; the next feature (a timestamp, an indent, a
prefix) makes it eight, and every single one lands in this method. The class violates the
open-closed principle in the most literal way: it must be reopened and edited for every new way to
dress a message. The Decorator pattern turns each dressing into its own small class that wraps any
other announcer, so combinations become wiring instead of branching.
There are two, and both feel like decoration. First: build the dressings by inheritance, a
ShoutingAnnouncer that extends PlainAnnouncer and overrides its output. That freezes the stack
into the type graph: banner-over-shout and shout-over-banner would need different classes, and the
sealed rules on the announcers refuse the whole idea. Second: write real wrapper classes but let a
decorator default its own inner (new PlainAnnouncer() inside ShoutingAnnouncer). Now the
decorator only decorates one thing, the "wraps any announcer" rules go red, and you have rebuilt
the hard-coded coupling one layer down.
Introduce IAnnouncer with a single Announce(message) method. PlainAnnouncer returns the
message untouched; it is the innermost layer. ShoutingAnnouncer and BannerAnnouncer each take
an IAnnouncer inner through the constructor and dress whatever the inner produced. All three are
sealed. The Solution class stacks them according to the flags: start plain, wrap in shouting if
asked, wrap in a banner if asked. Every output must match the flag version exactly.
Your submission must (1) still pass the behavior tests (identical strings for every flag
combination) and (2) satisfy every rule shown in the results: the abstraction and all
three announcers exist, all three implement IAnnouncer, all three are sealed, each decorator
holds the announcer it wraps in a readonly field, and neither decorator references the concrete
PlainAnnouncer. The inheritance version fails sealing; the self-defaulting version fails the
wraps-any rules; a dressing that stores no inner at all fails the readonly-field rules. Only true
composition, wired at the root, turns everything green.
A decorator adds one behavior to anything that honors the contract, including another decorator. The moment it names a concrete inner, it stops being a layer and becomes a lid.
How it is graded
Executable structural checks grade the shape of your types as well as their behavior.
What it practices
- Decorator and composite patterns
- Wrap or nest objects behind one interface so behaviors stack and trees evaluate uniformly.
- Composition over inheritance
- Combine small behaviors instead of multiplying subclasses or boolean flags.
Related katas
- 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.
- Caching Is an Adapter Medium Pro A quote use case memoizes prices with an infrastructure cache inside its own loop, so a storage tactic lives in the application. Move caching into a decorator on the price port, and let the composition root decide how long each cache lives.
- 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.