TypeScript Prototype Pattern Example

1. Definition

The Prototype pattern is a creational design pattern that allows objects to be cloned. This pattern involves implementing a prototype interface which tells to create a clone of the current object. This is done to avoid the overhead involved in creating a new instance traditionally.

2. Problem Statement

Creating an instance of a class can be more time-consuming and resource-intensive than copying an existing instance. Especially when the objects have numerous shared configurations and only differ slightly.

3. Solution

The Prototype pattern allows you to clone/copy an existing object without making the code dependent on their class. These cloned objects carry the same attributes as the original object and can be customized as required.

4. Real-World Use Cases

1. Graphic editor apps where shapes drawn can be cloned and replicated.

2. Cache mechanisms where data fetched can be cloned to avoid redundant fetch operations.

3. Any scenario where object initialization takes more resources than cloning.

5. Implementation Steps

1. Create a prototype interface that declares cloning methods.

2. The concrete class, from which objects will be cloned, implements this interface to clone its objects.

3. Client code creates a new object by asking the prototype to clone itself.

6. Implementation in TypeScript

// Step 1: Create a prototype interface
interface Prototype {
    clone(): Prototype;
    toString(): string;
}
// Step 2: The concrete class implementing the Prototype interface
class ConcretePrototype implements Prototype {
    private value: number;
    constructor(value: number) {
        this.value = value;
    }
    clone(): Prototype {
        return new ConcretePrototype(this.value);
    }
    toString(): string {
        return `Value: ${this.value}`;
    }
}
// Usage:
const originalPrototype = new ConcretePrototype(42);
const clonedPrototype = originalPrototype.clone();
console.log("Original:", originalPrototype.toString());
console.log("Cloned:", clonedPrototype.toString());

Output:

Original: Value: 42
Cloned: Value: 42

Explanation:

The Prototype pattern allows an object to be copied without knowing the specific type of the object. 

In our TypeScript implementation, ConcretePrototype implements the Prototype interface. It has a clone method that creates and returns a new instance of ConcretePrototype with the same value. When we clone the originalPrototype, we get a clonedPrototype with the same attributes. This pattern is especially useful when object creation is more costly than copying an existing object.

7. When to use?

The Prototype pattern is beneficial when:

1. Objects of a class can have one of only a few different combinations of state, making it more resource-efficient to clone a corresponding number of prototypes than to instantiate the class manually each time with the appropriate state.

2. Instances of a class are required to be unique in terms of attribute values, and this uniqueness can be ensured by cloning from a set of predefined prototypes.

3. Object initialization is more costly (in terms of time or resources) than cloning an existing instance.


Comments