Skip to content
Commitwise
Menu

Prototype pattern

Creating a new object by copying an existing one, such as a template, instead of building it from scratch.

Definition

The important decisions are how deep the copy goes and what must change in it. Nested mutable objects need to be copied or made immutable, otherwise the original and the copy share them; ids, numbers, dates and status must be reset so the copy is a new object. A prototype registry keeps preconfigured templates and always hands out copies. In Java, copy constructors are preferred over Cloneable.

In practice

Duplicating an invoice copies the client and line items but gets a new id, a new number and draft status.

Related terms

Esc

Type to search the whole site.