← Back to PatternPip

COMPOSITE / PRACTICE

One product. Nested gifts.
One way to ask the price.

Transfer the file-tree idea to a gift shop. Price individual products and nested boxes through one contract, then add a new kind of leaf.

YOUR MISSION / GIFT SHOP

One checkout.
Any depth of gift boxes.

Checkout must price one product or a box containing products and more boxes. Only contents contribute to the total: an empty box is $0, and there is no packaging fee. Build the design, test all three cases, then add a gift card.

File → Product · Folder → GiftBox · size() → price(). Groups still ask each child for the same operation. Prices are fictional integer-cent values in a local simulation.

01 / BUILD THE DESIGN

Make three design choices.

Choose how checkout calls an item, how a box combines its children, and where its total starts.

How should checkout ask for a total?

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

02 / TEST · TRACE · VERIFY

One item or a whole tree?

Local simulation

Verify all three cases with the same design. We check both the total and the common-contract boundary. A correct number alone can hide type-dependent code.

  • LEAF / INDIVIDUAL

    Mug

    —

    Own value: $12.00. No children to visit.

Choose all three design pieces to enable the trace.

Finish the trace to verify this case. Expected: $12.00.

See the code behind your design TypeScript

03 / THE FINAL CHALLENGE

A gift card joins the tree.

A gift card is a new leaf worth $25. It implements Priced directly; it does not inherit from Product. Which edits let it work inside the existing box?

Choose the two required edits, then press Apply edits & trace. Follow the gift card’s price back through its box to complete Practice.

Verify all three base cases with one design to unlock this extension.

Which two edits are needed?

0 of 2 edits selected

Compare with a complete solution

Hints and retries are unlimited. Revealing the answer does not verify any cases.