← Back to PatternPip

STRATEGY / PRACTICE

One checkout.
More ways to deliver.

Build a replaceable shipping rule. Predict which calculation runs, follow the quote, then add a new delivery method.

YOUR MISSION

New delivery methods. The same checkout.

Standard shipping works. Now customers want Express—and the ability to switch. Keep the formulas outside Checkout, then add free Pickup without changing its calculation.

Fixed package: 2 kg. Every strategy accepts whole kilograms and returns a nonnegative fee in integer cents.

01 / BUILD A REPLACEABLE BEHAVIOR

Make three design choices.

Give shipping one shared job, then decide who does it.

What job should every shipping method promise?

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

02 / PREDICT · QUOTE · OBSERVE

Switch the method. Keep Checkout.

Local simulation

Follow the 2 kg package from caller to Checkout and back. Only the selected strategy should calculate its fee.

CUSTOMER / CALLEROne package · 2 kgRequested: StandardAwaiting quote
CHECKOUT / CONTEXT

calculate(2)

Choose who calculates the feeDoes Checkout calculate, or delegate?
Standard

StandardShipping

500 + 100 × kg—Available implementationCustomer’s selection
Express

ExpressShipping

1000 + 200 × kg—Available implementation

Solid line = this quote’s call path. Dashed lines = available options, not calls to every strategy.

Optional prediction: who will calculate the fee?

Your choices drive the result. Predictions are optional.
See the code behind your design TypeScript

Fictional teaching prices. Calculations run synchronously; playback explains the selected execution. No real shipping service is called.

03 / TRANSFER THE IDEA

Now add free pickup.

PickupShipping returns 0 cents for the same package. Which parts need to change?

First verify Standard and Express with a delegating Checkout. This challenge opens when both pass.

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 whenever it helps.