TypeScript Prototype Pattern Example

In this article, we will learn how to use and implement the Prototype Pattern in TypeScript with an example.
Prototype is a creational design pattern that lets you copy existing objects without making your code dependent on their classes.

TypeScript Prototype Pattern Example

The below diagram shows the generic structure of the Prototype Pattern:
Let's refer to the above structure to create an example to demonstrates the usage of the Prototype Pattern.
Let's create prototype.ts file and add the following code to it:
export interface Prototype {
    clone(): Prototype;
    toString(): string;
}

export class Concrete1 implements Prototype {

    clone() : Prototype {
        return new Concrete1();
    }

    toString(): string {
        return "This is Concrete1";
    }
}

export class Concrete2 implements Prototype {

    clone() : Prototype {
        return new Concrete2();
    }

    toString(): string {
        return "This is Concrete2";
    }
}

export class Concrete3 implements Prototype {

    clone() : Prototype {
        return new Concrete3();
    }

    toString(): string {
        return "This is Concrete3";
    }
}


export class Builder {
    private prototypeMap: { [s: string]: Prototype; } = {};

    constructor() {
        this.prototypeMap['c1'] = new Concrete1();
        this.prototypeMap['c2'] = new Concrete2();
        this.prototypeMap['c3'] = new Concrete3();
    }

    createOne(s: string): Prototype {
        console.log(s);
        return this.prototypeMap[s].clone();
    }
}

Usage

Let's create demo.ts file and add the following code to it:
import { Builder } from "./prototype";

export function show() : void {
 var builder : Builder = new Builder();
 var i = 0;
 for (i = 1; i <= 3; i += 1) {
  console.log(builder.createOne("c" + i).toString());
 }
}
show();
Run:
  • Compile the above code using the TypeScript compiler.
  • Above code is compiled to plan JavaScript code
  • Run Javascript code using node
design_patterns_in_typescript\prototype> tsc --target ES5 .\demo.ts
design_patterns_in_typescript\prototype> node .\demo.js
c1
This is Concrete1
c2
This is Concrete2
c3
This is Concrete3

All TypeScript Design Patterns

1. Creational Design Patterns

Creational patterns provide various object creation mechanisms, which increase flexibility and reuse of existing code.
  1. TypeScript Singleton Pattern Example
  2. TypeScript Factory Design Pattern with Example
  3. TypeScript Abstract Factory Pattern Example
  4. TypeScript Builder Pattern Example
  5. TypeScript Prototype Pattern Example

2. Structural Design Patterns

Structural patterns explain how to assemble objects and classes into larger structures while keeping these structures flexible and efficient.
  1. TypeScript Bridge Pattern Example
  2. TypeScript Adapter Pattern Example
  3. TypeScript Decorator Pattern Example
  4. TypeScript Composite Pattern Example
  5. TypeScript Flyweight Design Pattern Example
  6. TypeScript Facade Pattern Example
  7. TypeScript Proxy Pattern Example

3. Behavioral Design Patterns

Behavioral design patterns are concerned with algorithms and the assignment of responsibilities between objects.
  1. TypeScript Command Pattern Example
  2. TypeScript Chain of Responsibility Pattern Example
  3. TypeScript Visitor Pattern Example
  4. TypeScript Template Method Pattern Example
  5. TypeScript Strategy Pattern Example
  6. TypeScript State Pattern Example
  7. TypeScript Observer Pattern Example
  8. TypeScript Memento Pattern Example
  9. TypeScript Mediator Pattern Example
  10. TypeScript Iterator Pattern Example
  11. TypeScript Interpreter Design Pattern Example

Comments