Singleton class



    The Singleton class contains a single instance of the class. This instance is stored in a variable named Singleton.singleton. The Singleton class has a private constructor so that no other instances of the class are created. The public static getInstance() method returns the singleton instance.

    export class Singleton {
      // A variable that stores the singleton object. Initially,
      // the variable acts like a placeholder
      private static singleton: Singleton;
      // private constructor so that no instance is created
      private constructor() {
      // This is how we create a singleton object
      public static getInstance(): Singleton {
        // check if an instance of the class is already created
        if (!Singleton.singleton) {
          // If not created create an instance of the class
          // store the instance in the variable
          Singleton.singleton = new Singleton();
        // return the singleton object
        return Singleton.singleton;
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.