“Singleton” Kode Jawaban

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

pola singleton

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

Jawaban yang mirip dengan “Singleton”

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya