Prototype Class

    2

    0

    Giovanny Gongora

    Codiga's TypeScript Recipes

    Prototype is a creational design pattern that lets you copy existing objects without making your code dependent on their classes.

    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 Builder {
      private prototypeMap: { [s: string]: Prototype; } = {};
      constructor() {
        this.prototypeMap['c1'] = new Concrete1();
      }
      createOne(s: string): Prototype {
        console.log(s);
        return this.prototypeMap[s].clone();
      }
    }
    
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    Legal
    • Security
    • Privacy Policy
    • Code Privacy
    • Terms of Service
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.