Prototype (Creational)
Intent / problem it solves
Create new objects by cloning an existing instance instead of building from scratch. Useful when construction is expensive or you want to preserve default state trees.
When to use / when NOT
Use when duplicate objects differ slightly, or deep graphs should be copied with controlled sharing.
Avoid when immutable values or factories are simpler, or when shallow copies would leak shared mutable internals.
Structure
Prototype declares clone(); concrete prototypes implement copy semantics (shallow or deep).
Go example
package main
import "fmt"
type Theme struct {
name string
accentHue int
}
func (theme Theme) Clone() Theme {
return Theme{name: theme.name + "-copy", accentHue: theme.accentHue}
}
func main() {
original := Theme{name: "dark", accentHue: 210}
duplicate := original.Clone()
fmt.Println(original.name, duplicate.name)
}JavaScript example
class DocumentTemplate {
constructor(title, sections) {
this.title = title;
this.sections = sections.map((section) => ({ ...section }));
}
clone() {
return new DocumentTemplate(this.title + ' (copy)', this.sections);
}
}
const base = new DocumentTemplate('Sprint plan', [
{ heading: 'Goals', body: 'Ship MVP' },
]);
const derived = base.clone();
derived.sections[0].body = 'Ship beta';
console.log(base.sections[0].body, derived.sections[0].body);Interview phrase
“Prototype avoids recomputing heavy defaults: you clone a prepared instance and tweak deltas; watch for deep vs shallow copy of nested collections.”
Related LLD case studies
Tie to LLD for template documents, game level blueprints, or cached plan reuse in LLD case studies.
Last updated on
Spotted something unclear or wrong on this page?