Skip to content

Latest commit

 

History

History
76 lines (56 loc) · 1.24 KB

File metadata and controls

76 lines (56 loc) · 1.24 KB

Singleton

Ensure a class only has one instance and provide a global point of access to it

classDiagram
direction LR

Singleton


class Singleton {
 + Instance
 - Singleton()
 + GetInstance()
}

Loading

implemetation in Python:

WIKIPEDIA BUILDER PATTERN

class Singleton:
    _instance = None

    def __new__(cls):
        if cls._instance is None:
            cls._instance = super().__new__(cls)
        return cls._instance

    def __init__(self):
        self.state = "initialized"

    def some_method(self):
        print(f"Singleton state: {self.state}")

if __name__ == "__main__":
    s1 = Singleton()
    s2 = Singleton()

    print(f"s1 is s2: {s1 is s2}")

    s1.state = "modified"
    s2.some_method()

Implementation in JavaScrip:

class Singleton {
  static instance;

  constructor() {
    if (Singleton.instance) {
      return Singleton.instance;
    }

    this.state = "initialized";
    Singleton.instance = this;
  }

  someMethod() {
    console.log(`Singleton state: ${this.state}`);
  }
}

// Usage
const s1 = new Singleton();
const s2 = new Singleton();

console.log(`s1 is s2: ${s1 === s2}`);

s1.state = "modified";
s2.someMethod();