Strategy: shipping-rate estimation
One shipment, three interchangeable rate strategies, priced identically and proven by GoogleTest.
A checkout needs to compute a delivery cost, and the rate rule depends on the
carrier. This demo models a small shipping domain (a Shipment with a weight,
a destination zone, and a declared value, priced in integer Cents) and shows
the Strategy pattern in three idiomatic C++ expressions, all pricing an
identical shipment identically:
- Runtime polymorphism. An abstract
RateStrategywithFlatRateCarrier,WeightBasedCarrier, andTieredZoneCarrier, injected into aRateEstimatorthrough astd::unique_ptrand swapped at runtime. - Value-semantic
std::function. ARateFnheld by value, built by theflat_rate/weight_based/tiered_zonefactories, withwith_insurance_surcharge()composing a new strategy on top of an existing one at runtime. - Compile-time policy. A
RatePolicyconcept and aStaticRateCalculator<Policy>that selects the strategy at compile time with no indirection.
A C++23 refinement (try_estimate() returning std::expected<Cents, std::string>)
is included behind a feature guard, so the demo still builds under C++20.
Build, run, observe
From the repo root, with the toolchain from the prerequisites chapter:
./scripts/build.sh cpp23
./scripts/verify.sh cpp23 # 18/18 tests pass
./examples/08-strategy/demo.sh cpp23
The demo prints the computed costs for each expression (for example a
weight-based quote of $10.25 and a tiered-zone quote of $12.30), shows a
runtime strategy swap, composes an insurance surcharge onto a std::function
strategy, and rejects a malformed shipment through the typed std::expected
path. Chapter 8 walks through how each expression works and when to reach for
which one.
The demo is also standalone-buildable from its own directory with
conan install and cmake --preset cpp23, and it builds under the cpp20
preset (16 tests, with the std::expected cases compiled out).