“Singleton” Code-Antworten

Singleton

public final class Singleton {

  private static final Singleton INSTANCE = new Singleton();

  private Singleton() {}

  public static Singleton getInstance() { 
    return INSTANCE;
  }
}
Adorable Anteater

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;
Foolish Flamingo

Singleton -Muster

class Singleton(object):
    __instance = None
    def __new__(cls, *args):
        if cls.__instance is None:
            cls.__instance = object.__new__(cls, *args)
        return cls.__instance
ProMikeSundays

Ähnliche Antworten wie “Singleton”

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen