Answers for "define singleton"

0

singleton

let instance;
let counter = 0;

class Counter {
  constructor() {
    if (instance) {
      throw new Error("You can only create one instance!");
    }
    instance = this;
  }
}

const singletonCounter = Object.freeze(new Counter());
export default singletonCounter;
Posted by: Guest on December-21-2021
1

singleton pattern

class Singleton(object):
    __instance = None
    def __new__(cls, *args):
        if cls.__instance is None:
            cls.__instance = object.__new__(cls, *args)
        return cls.__instance
Posted by: Guest on October-18-2020

Browse Popular Code Answers by Language