19
Chain of Responsibility and Iterator
An order-approval handler chain and a hand-written forward iterator that also works as a C++20 range.
Two traversal/coordination patterns:
- Chain of Responsibility. An
ApprovalHandlerchain (weight limit, hazmat, embargo, then a manual-approval fallback) where each handler either acts or passes the request on. A call log records exactly how far each request travelled, so tests assert the path, not just the final decision. - Iterator.
PackageManifeststores packages in four zone buckets but exposes a hand-written forward iterator that flattens them into zone order.static_assert(std::forward_iterator<...>)andstatic_assert(std::ranges::forward_range<...>)prove it is a real range, sostd::views::filterandstd::views::transformcompose over it directly.
The C++23 codetabs path ends the ranges pipeline with std::ranges::to<std::vector>()
(vs a manual iterator-pair construction in C++20).
Build, run, observe
./scripts/build.sh cpp23
./scripts/verify.sh cpp23 # 9/9 tests pass
./examples/19-chain-iterator/demo.sh cpp23
The demo routes several shipments through the approval chain, printing where
each stops, then traverses the manifest with the iterator and with a ranges
pipeline. Chapter 19 covers when each pattern fits. Builds standalone and under
the cpp20 preset (8 tests).