← Back to PatternPip

DECORATOR / PRACTICE

One line of text.
Layers of possibility.

You met Decorator through notification senders. Now compose optional extras around a text preview.

YOUR MISSION / TEXT PREVIEWS

Add an extra.
Keep the same client.

A preview starts with “hello”. Some callers want a label, some want uppercase, and some want both. Keep the client calling render() while setup chooses the wrappers and their order.

Each layer receives text from its inner object, then adds its own behavior. Test both orders and add a border. This is object composition, not @decorator syntax.

01 / DESIGN A WRAPPER

Make three design choices.

Both LabelText and UppercaseText use your choices. Keep the contract, delegate, then add an extra.

How can every layer fit the same client?

Try your own design first. Hints and retries are unlimited.

02 / WRAP · CALL · RETURN

Same wrappers. Different order.

Local simulation

LabelText(PlainText) should add Note: to hello.

Predict first (optional): What does a correct label wrapper return?
COMPOSED OUTSIDE → INSIDE

Calls travel down. Each layer returns text back up.

UNCHANGED CLIENT

Client · showPreview

0 calls

Calls render() through TextView

Return value—
call ↓ · return ↑
WRAPPER 1

LabelText

0 calls

Adds “Note: ” after its inner object returns

Return value—
call ↓ · return ↑
ORIGINAL COMPONENT

PlainText

0 calls

Supplied object · owns the original “hello”

Return value—
PREVIEW RECEIVED BY CLIENTFollow the return path to see the final text.

Original object rendered 0 times so far.

Step through, or play and pause. Predictions are optional.

Start a trace to test your selected wrappers. Matching text alone does not prove correct delegation.

Each test uses a fresh chain. Back replays recorded events. Changing a design choice clears all verification.

See the code behind your design TypeScript

03 / EXTEND THE CHAIN

One more extra: a border.

Add BorderText outside the uppercase-and-label chain. It should turn NOTE: HELLO into [NOTE: HELLO]. Which edits are needed?

First finish passing traces for the label and both wrapper orders with the same assembly.

Select all required edits

Compare with a complete solution

No timer and no attempt limit. Try another assembly, use a hint, or reveal the answer when it helps.