How to Implement Design Patterns in TypeScript: Prototype

Amir Elemam
3 min readJul 29, 2023

--

What is the Prototype Design Pattern?

The Prototype design pattern is a creational design pattern in software development. It is used when the type of objects to create is determined by a prototypical instance, which is cloned to produce new objects.

This pattern is used to:

  • Avoid subclasses of an object creator in the client application, like the abstract factory pattern does.
  • Avoid the inherent cost of creating a new object in the standard way (e.g., using the ‘new’ keyword) when it is prohibitively expensive for a given application.

To implement the pattern, you declare an abstract base class that specifies a pure virtual clone method. Any class that needs a “polymorphic constructor” capability derives itself from the abstract base class, and implements the clone operation.

The client, instead of writing code that invokes the “new” operator on a hard-coded class name, calls the clone method on the prototype, calls a factory method with a parameter designating the particular concrete derived class to be instantiated, or invokes the clone method through some mechanism provided by another design pattern.

The Prototype design pattern offers several advantages:

Performance: If creating a new object is a costly operation, prototype design pattern helps in improving performance by cloning/copying the existing objects, instead of creating new ones.

Dynamic Configuration: This pattern enables a more dynamic configuration of an application. Instead of hard coding the type of object to create, the application can clone objects at runtime.

Variety of Derived Classes: The pattern allows the client to create copies of arbitrary concrete objects, not just instances of a family related by a common interface. Hence, it can be used to instantiate a variety of derived classes.

Preserve the existing state: When an object is cloned, the cloned object carries the state of the original object. This can be helpful if you want to create a new object with an existing object’s state.

Ease of use with complex objects: For complex objects that take a lot of time to instantiate and set up, and that might also require complex API interactions, prototypes can drastically simplify and speed up the process of creating more of these objects.

However, keep in mind that this pattern has its trade-offs. Deep cloning can be a costly operation for complex objects and can consume a lot of memory, and it can be tricky when dealing with objects that have circular references or contain resources that can’t be copied easily, like file handles or database connections.

How to Implement Prototype?

In TypeScript, the Prototype pattern can be implemented using a clone method on a class. Here is a simple example:

interface Prototype {
clone(): Prototype;
sayHello(): void;
}

class ConcretePrototype implements Prototype {
private message: string;

constructor(message: string) {
this.message = message;
}

public clone(): Prototype {
return new ConcretePrototype(this.message);
}

public sayHello(): void {
console.log(this.message);
}
}

function clientCode(): void {
const prototype1: Prototype = new ConcretePrototype('Hello, World!');
prototype1.sayHello();

const prototype2: Prototype = prototype1.clone();
prototype2.sayHello();
}

clientCode();

In this example, ConcretePrototype is the class that gets cloned. The clone method creates a new ConcretePrototype using the same message as the current instance.

The clientCode function creates an instance of ConcretePrototype and then clones it. Despite prototype1 and prototype2 being different instances, they were created without directly using the new keyword on ConcretePrototype inside clientCode. This means the exact type of the object doesn't have to be known by clientCode, following the Prototype pattern.

This is a very simplified example. In a real project, the Prototype pattern might be useful for complex objects that are expensive to create or when you need to create objects dynamically at runtime.

--

--