12
Factory Method and Abstract Factory
Carrier integrations built as matching product families, with the call site never naming a concrete carrier.
A carrier-integration example for two carriers (FastShip and SummitFreight):
- Abstract Factory.
CarrierIntegrationFactorydeclares a family (make_label_formatter,make_rate_quoter,make_tracking_client); each concrete factory produces an internally consistent family.ShipmentProcessorbuilds all three from one factory and never names a concrete carrier class. - Factory Method.
CarrierOnboarder::onboard()runs a fixed sequence and calls a private virtualcreate_test_suite();SandboxOnboarderandProductionOnboarderoverride just that hook.
A C++23 try_make_carrier_factory() returning std::expected is guarded so the
C++20 build still works.
Build, run, observe
./scripts/build.sh cpp23
./scripts/verify.sh cpp23 # 14/14 tests pass
./examples/12-factories/demo.sh cpp23
The demo processes a shipment through one carrier’s whole product family and
onboards a carrier in sandbox and production modes. Chapter 12 distinguishes
Factory Method from Abstract Factory and when each fits. Builds standalone and
under the cpp20 preset (12 tests).