Vzor Singleton zajišťuje, že třída má pouze jednu instanci a poskytuje globální přístupový bod k ní. Používá se, když přesně jeden objekt by měl koordinovat něco (konfiguraci, fond připojení, logger), ačkoli je to také poněkud kontroverzní.
Co Singleton dělá
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
Příklad
{
#instance;
() {
(!.#instance) {
.#instance = ();
}
.#instance;
}
}
