← Home
🏗Crack System Design with DSA AnimatorSystem DesignNEW
🏗 System Design›Creational Patterns

🧬Prototype Pattern

Specify the kinds of objects to create using a prototypical instance — clone it instead of calling complex constructors.

📌 Key IdeaAvoid expensive setup by cloning a configured template (prototype). Common when object graphs are heavy, subclasses differ only slightly, or construction would duplicate registry lookups.
✨

Why It Matters

  • Performance — Skip repeated initialization work — copy existing state.
  • Flexibility — Add new prototype instances without new subclasses.
  • Registry — Central catalogue of template objects (e.g. cell styles in a spreadsheet app).
🖼

Visual

🧬 Clone vs `new`
  Document draft = registry.get("invoice-template").clone();
  draft.setCustomerId(42);  // cheaper than rebuilding graph

  // Java: clone(), copy ctor, or serialization copy

Registry holds archetypes; clients obtain copies and tweak deltas.

✅

When to Use

  • Instantiation is costly (DB hydration, parsing configs, deep graphs).
  • Many objects differ only slightly from a template (game entities, documents).
  • You want to avoid subclass explosion — compose variants by cloning + mutate.
  • Framework needs runtime type selection — prototype registry by key.
🚫

When NOT to Use

  • Shallow copy would share mutable internals incorrectly — need deep copy discipline.
  • Objects aren't cloneable by contract — immutability + factory may be simpler.
  • Cloning is harder to reason about than explicit builders for some teams.
🚨

Anti-Pattern (What NOT to Do)

Rebuild everythingBadjava
Invoice makeInvoice() {
    return new Invoice(loadTaxTable(), loadTemplates(),
        fetchCompanyHeader()); // slow + duplicated
Every request pays full construction cost.
✅

The Right Way

Prototype + shallow tweakGoodjava
Invoice copy = templateInvoice.clone();
copy.setLineItems(itemsForRequest);
return copy;
Ensure clone() does correct deep/shallow semantics for your graph.
⚖️

Pros & Cons

👍 Pros
  • Hides concrete product classes from the client (via prototype interface).
  • Can reduce subclass counts — vary by composing clones.
  • Useful with object pools and template registries.
👎 Cons
  • Implementing correct cloning (especially deep copy) is subtle in Java.
  • Cloneable / clone() are awkward — often prefer copy constructors or serialization.
🔀

Common Confusions Cleared Up

Prototype vs Factory Method

Factory Method constructs via subclass-specific logic. Prototype duplicates an existing object — creation cost moves to clone implementation.

⚠️

Common Mistakes / Anti-Patterns

  • Shallow clone of collections when elements must be duplicated too.
  • Mutable shared singleton accidentally cloned — identity bugs.
🎤

Interview Q&A

🔮

Likely Follow-up Questions

  • How does Spring prototype scope relate to this GoF pattern?
  • Registry of named prototypes — where have you used it?
🧾

Quick Cheat Sheet

IntentClone template; avoid costly new paths.
Java pitfallclone() / Cloneable design flaws.
vs FactoryCopy existing vs construct via factory hook.
Deep vs shallowMust match business invariants.
🔗

Related Topics