Skip to content
Katabench
Try free

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.

The trap

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.

Your job

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.

How it's graded

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.

SOLID principles exercises →

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