Singleton-mönstret säkerställer att en klass har bara en instans och tillhandahåller en global åtkomstpunkt till den. Det används när exakt ett objekt ska koordinera något (en konfiguration, en anslutningspool, en logger), även om det också är något kontroversiellt.
Vad Singleton gör
SINGLETON → guarantee a class has only ONE instance, with global access to it:
→ the class controls its own instantiation (private constructor)
→ returns the SAME instance every time it's requested
→ for: things there should only be ONE of, accessed from many places
Exempel
{
#instance;
() {
(!.#instance) {
.#instance = ();
}
.#instance;
}
}
