Skip to main content

🧩 Prototype Pattern

✅ Intent​

  • Reuse pre-initialized logic and structure by duplicating a prototype instance
  • Clone a base object using clone() and apply modifications only where needed

✅ Motivation​

  • When you have logic or structures that are "almost the same", cloning a template improves maintainability and reusability
  • Ideal when many similar instances are created from scratch using new, and you want to centralize shared setup

✅ When to Use​

  • When the initial state is mostly shared, and only a few properties differ
  • Useful for duplicating forms, configuration objects, notification templates, UI layouts, etc.

✅ Code Example​

interface ExporterPrototype {
clone(): ExporterPrototype;
export(data: string): void;
}

class PdfExporter implements ExporterPrototype {
constructor(private withLog: boolean = false) {}

clone(): ExporterPrototype {
return new PdfExporter(this.withLog);
}

export(data: string): void {
if (this.withLog) console.log("開始ログ");
console.log(`PDF出力: ${data}`);
if (this.withLog) console.log("完了ログ");
}

enableLogging(): void {
this.withLog = true;
}
}

// 利用例
const baseExporter = new PdfExporter();
baseExporter.enableLogging();

const cloned = baseExporter.clone();
cloned.export("請求書");

✅ Explanation​

This code applies the Prototype pattern to clone an existing object
and generate a new instance with the same properties.
The Prototype pattern creates new objects by copying an existing instance,
instead of instantiating a new one from its class directly.

1. Overview of the Prototype Pattern​

  • Prototype: Interface that provides a cloning method

    • Represented by ExporterPrototype in this code
  • ConcretePrototype: Implements the prototype interface and provides cloning logic

    • Represented by PdfExporter
  • Client: Uses a prototype object and clones it to create new instances

    • Represented by the call to baseExporter.clone()

2. Key Classes and Their Roles​

  • ExporterPrototype

    • Common interface for prototypes
    • Declares clone(): ExporterPrototype for creating a copy
    • Declares export(data: string): void for output functionality
  • PdfExporter

    • A concrete prototype class
    • Implements clone to return a copy of itself
    • Implements export to output data in PDF format
    • Provides an enableLogging method to customize cloned instances

3. UML Class Diagram​

4. Benefits of the Prototype Pattern​

  • Flexible Object Creation: Create new instances based on existing objects without relying on constructors
  • Avoids Repetitive Initialization: Use clones to avoid repeating complex setup logic
  • Extensibility: Adding a new class only requires implementing the clone method

This design is highly effective when new objects need to be based on existing ones,
improving both flexibility and reusability of the codebase.