Skip to main content

๐Ÿงฉ Prototype Pattern

โœ… Intentโ€‹

  • Create new objects by cloning a prototype rather than instantiating from scratch
  • Reuse a pre-configured object as a base and apply only the necessary differences

โœ… Motivationโ€‹

  • Avoid hardcoding the entire configuration of an object every time
  • Instead, maintain a single source of truth and clone it for variations

โœ… When to Useโ€‹

  • When object creation involves expensive setup or many default values
  • You need to generate many similar instances with minor differences
  • You're dealing with notification templates, configuration sets, or UI components

โœ… Code Exampleโ€‹

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

class Notification implements Cloneable<Notification> {
constructor(
public header: string,
public footer: string,
public user: string = "",
public message: string = ""
) {}

clone(): Notification {
return new Notification(this.header, this.footer);
}

send(): void {
console.log(
`${this.header}\nๅฎ›ๅ…ˆ: ${this.user}\nๆœฌๆ–‡: ${this.message}\n${this.footer}`
);
}
}

// ใƒ—ใƒญใƒˆใ‚ฟใ‚คใƒ—็”Ÿๆˆ
const prototype = new Notification("ไปถๅ: ใŠ็Ÿฅใ‚‰ใ›", "-- ้€š็Ÿฅใ‚ทใ‚นใƒ†ใƒ ");

// ๅˆฉ็”จไพ‹
const n1 = prototype.clone();
n1.user = "hiroshi";
n1.message = "ใ“ใ‚“ใซใกใฏ";
n1.send();

const n2 = prototype.clone();
n2.user = "satoshi";
n2.message = "ใŠ็Ÿฅใ‚‰ใ›ใŒใ‚ใ‚Šใพใ™";
n2.send();

โœ… Explanationโ€‹

This code uses the Prototype pattern to create new Notification objects by cloning a pre-initialized instance.

Each clone inherits all base properties and logic (e.g. headers, formatting rules), and can then be customized with user-specific content before sending.

1. Prototype Pattern Overviewโ€‹

  • Prototype: An interface or base class that defines a cloning method
    โ†’ Cloneable<T>

  • ConcretePrototype: Implements the cloning logic and serves as the base for duplication
    โ†’ Notification

  • Client: Clones the prototype and updates only the necessary fields before use

2. Key Classes and Responsibilitiesโ€‹

  • Cloneable<T>

    • Defines the clone() method
    • Ensures each prototype instance can be duplicated
  • Notification

    • Implements Cloneable
    • Provides clone() and send() methods
    • Base template can be cloned and reused with customized content
  • Client Code

    • Creates a base prototype with fixed settings
    • Clones it for each user/message and customizes as needed

3. UML Class Diagramโ€‹

4. Benefits of the Prototype Patternโ€‹

  • Avoids duplication: Write setup logic once and reuse it through cloning
  • Efficient object creation: Especially useful when instantiation is complex or resource-intensive
  • Flexible extension: Add variations by modifying clones instead of subclassing

This design is ideal for building systems that need to generate multiple pre-configured objects with slight variations, such as email templates, UI components, or configuration snapshots.