13
Builder, Prototype, and serialization
A freight manifest assembled with a validating builder, cloned from prototypes, and round-tripped through text.
A freight shipment manifest assembled three ways that cooperate:
- Builder.
ManifestBuilderhas fluent chained setters, defaults the optional fields, and validates once atbuild(), throwing and naming the missing field on an incomplete manifest. - Prototype.
PackageTemplateRegistryholds named prototypes (fragile-electronics,bulk-dry-goods,refrigerated-perishable);clone()is a true deep copy because every member is a value type. - Serialization. A plain
KEY=VALUE+PACKAGE ...text format; serialize then deserialize reproduces a structurally equal manifest.
The C++23 codetabs path contrasts std::format (both standards) with
std::println (C++23), guarded so the C++20 build still works.
Build, run, observe
./scripts/build.sh cpp23
./scripts/verify.sh cpp23 # 19/19 tests pass
./examples/13-builder-prototype/demo.sh cpp23
The demo builds a manifest, rejects an incomplete one, clones a prototype and
mutates the clone without touching the original, and round-trips a manifest
through text. Chapter 13 covers when cloning beats construction. Builds
standalone and under the cpp20 preset (17 tests).