Il-Singleton pattern jiżgura li klassi għandha instance wieħed biss u tipprovdi punt ta' aċċess globali lejn. Jintuża meta eżattament oġġett wieħed għandu jkoordina xi ħaġa (konfigurazzjoni, connection pool, logger), għalkemm hu wkoll xi ħadd kontroversja.
X'jagħmel Singleton
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
Eżempju
{
#instance;
() {
(!.#instance) {
.#instance = ();
}
.#instance;
}
}
