16
Decorator and Proxy
Stackable rate decorators and access-controlling and lazy proxies over the same interfaces they wrap.
Two same-interface wrapper patterns with different intent:
- Decorator. A
RateSourcecore wrapped in stackable layers (LoggingRateSource,CachingRateSource,SurchargeRateSource). Order matters:Surcharge(Caching(Logging(Core)))recomputes the surcharge every call while the base stays cached, so a rate change takes effect immediately;Caching(Surcharge(...))caches the already-surcharged total, so the same change has no effect until the key changes. Both are tested. - Proxy. A
ManifestStorefronted by aLazyManifestStoreProxy(defers building the real subject until first use) and anAccessControlledManifestProxy(gates lookups by access level).
A C++23 try_lookup() returning std::expected is guarded for the C++20 build.
Build, run, observe
./scripts/build.sh cpp23
./scripts/verify.sh cpp23 # 14/14 tests pass
./examples/16-decorator-proxy/demo.sh cpp23
The demo quotes through two decorator orders to show the difference, then
exercises the lazy and access-controlled proxies. Chapter 16 distinguishes
Decorator from Proxy (same interface, different intent). Builds standalone and
under the cpp20 preset (12 tests).