13

Builder and Prototype: assembling a complex shipment

A fluent builder that validates once at build time, records with wither copies, and a prototype cloned from an exemplar.

Demo 13Source on GitHub ↗

Builder makes construction read as a sequence of choices and validates the result once; Prototype makes a new object by copying a configured exemplar. This module assembles a Shipment with many optional parts through a Bloch-style static nested Builder (with a cross-field rule that an international destination requires a customs declaration), uses a record with wither copies for a CustomsDeclaration, and clones a PackagingTemplate exemplar into independent copies.

The JUnit 5 tests prove the builder rejects an invalid combination and builds a valid object, a wither produces an independent changed copy, and a prototype copy is independent of its exemplar and its siblings.

Build, run, observe

cd examples/13-builder-prototype
mvn -q test
./demo.sh

The module also runs from the aggregator root with mvn -q -f examples/pom.xml test.