intermediate
Prototype
Create new objects by cloning a configured exemplar when setup is expensive or concrete classes should remain hidden.
Prototype creates new objects by cloning an existing exemplar instead of running expensive setup or exposing concrete classes. It fits when configuration is heavy, when subclasses should stay hidden, or when you need many near-duplicate instances such as document templates or game entities.
On interviews: relate Prototype to `structuredClone`, copy constructors, and immutable snapshots. Mention deep versus shallow copy pitfalls.
Common pitfalls: shallow clones sharing mutable nested state; cloning objects with open resources; prototype registries without version discipline.
Checklist:
- Define copy semantics explicitly.
- Hide concrete classes behind clone interface.
- Benchmark setup cost before introducing pattern.
- Document which fields are safe to share.