Skip to content

Singleton Pattern

Singleton Pattern

A Singleton Pattern is a design pattern that allows us to use a single class everywhere.A super basic implementation could be:

class Rookie {
  time = new Date();

  constructor() {
    console.log(this);
  }
}

const Singleton = (function () {
  let instance: Rookie;

  return {
    getInstance: function() {
      if (!instance) instance = new Rookie();

      return instance;
    }
  }
})()

// getting the instance is pretty obvious