Singleton deseni, bir sınıfın yalnızca bir instance'ı olmasını garantiler ve ona global bir erişim noktası sağlar. Tam olarak bir nesnenin bir şeyi koordine etmesi gerektiğinde kullanılır (bir yapılandırma, bir bağlantı havuzu, bir logger), ancak bu da biraz tartışmalıdır.
Singleton ne yapar
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
Örnek
{
#instance;
() {
(!.#instance) {
.#instance = ();
}
.#instance;
}
}
