Prototype

Create new objects by copying a configured example, instead of building each one from scratch.

Description

A prototype is an object that knows how to copy itself through a clone() method. You set up a few well-configured examples, such as contract templates, game units or default settings, and create new objects by cloning them and changing what differs.

Often the prototypes live in a registry keyed by name, so callers ask for create("nda") without knowing the concrete class or how it was configured. The crucial detail is the depth of the copy: a clone must not share mutable parts with its template.

Class diagram

holds templatesTemplateRegistry- templates: Map<string, ContractDoc>+ register(name, template)+ create(name): ContractDoc«interface»Prototype<T>+ clone(): TContractDoc+ title: string+ sections: string[]+ meta: Record<string, string>+ clone(): ContractDoc

Prototype pattern class diagram

  1. Class TemplateRegistry with + register(name, template), + create(name): ContractDoc.
  2. Interface Prototype<T> with + clone(): T.
  3. Class ContractDoc with + clone(): ContractDoc.
  4. TemplateRegistry holds templates ContractDoc.
  5. ContractDoc implements Prototype<T>.

Sequence diagram

ClientTemplateRegistryNDA templateAcme copy1. create("nda")2. clone()3. new ContractDoc(copied fields)4. copy5. copy6. title = "Mutual NDA: Acme Ltd"7. sections.push("Governing law")

Prototype pattern sequence diagram

  1. Client → TemplateRegistry: create("nda").
  2. TemplateRegistry → NDA template: clone().
  3. NDA template → Acme copy: new ContractDoc(copied fields).
  4. TemplateRegistry receives "copy" back from NDA template.
  5. Client receives "copy" back from TemplateRegistry.
  6. Client → Acme copy: title = "Mutual NDA: Acme Ltd".
  7. Client → Acme copy: sections.push("Governing law").

TypeScript example

A registry of contract templates. Each new contract is a deep-enough copy of a template, so editing the copy never changes the template or other copies.

prototype.ts
// Prototype: new contracts start as copies of configured templates.

interface Prototype<T> {
  clone(): T;
}

class ContractDoc implements Prototype<ContractDoc> {
  title: string;
  sections: string[];
  meta: Record<string, string>;

  constructor(title: string, sections: string[], meta: Record<string, string>) {
    this.title = title;
    this.sections = sections;
    this.meta = meta;
  }

  // Copy deeply enough that changing the copy never touches the template.
  clone(): ContractDoc {
    return new ContractDoc(this.title, [...this.sections], { ...this.meta });
  }
}

// A registry of named prototypes
class TemplateRegistry {
  private readonly templates = new Map<string, ContractDoc>();

  register(name: string, template: ContractDoc): void {
    this.templates.set(name, template);
  }

  create(name: string): ContractDoc {
    const template = this.templates.get(name);
    if (!template) throw new Error(`no template called "${name}"`);
    return template.clone();
  }
}

export function demo(): string[] {
  const registry = new TemplateRegistry();
  registry.register("nda", new ContractDoc("Mutual NDA", ["Parties", "Confidential information", "Term"], { lang: "en", reviewedBy: "legal" }));

  const acme = registry.create("nda");
  acme.title = "Mutual NDA: Acme Ltd";
  acme.sections.push("Governing law: Sweden");

  const globex = registry.create("nda");
  return [
    `${acme.title}: ${acme.sections.length} sections`,
    `${globex.title}: ${globex.sections.length} sections (template untouched)`,
    `reviewed by ${globex.meta.reviewedBy}`,
  ];
}

When to use it

Use it when

  • Objects are expensive or fiddly to configure, and most new ones are small variations of a few templates.
  • Users or admins define the templates at runtime, so you can't hard-code a class per variant.
  • You need copies of an object while keeping the original unchanged, such as drafts, undo snapshots or what-if scenarios.

Avoid it when

  • Objects are cheap to create with a constructor or a builder. Copying adds nothing.
  • Objects hold resources that can't be duplicated safely: open connections, file handles, locks.
  • Plain data is enough. structuredClone(obj) or a spread copy does the job without a pattern.

Pitfalls

  • Shallow copies. Copying the object but not its arrays and nested objects means the clone and the template change together. Copy every mutable part, or use structuredClone.
  • Cloning objects with identity, such as IDs or database keys, and ending up with duplicates. Reset identity fields in clone().
  • Templates drifting silently when someone edits the prototype itself. Freeze templates, or keep them in a registry that only hands out copies.

Alternatives

structuredClone / spread

Choose it when you're copying plain data objects; JavaScript copies them without a clone method.

Builder

Choose it when new objects differ a lot, and are best assembled step by step.

Factory Method

Choose it when the choice is which class to create, not which configured example to copy.

Memento

Choose it when you copy state to restore it later, rather than to create new objects.