Singleton pattern یقینی بناتا ہے کہ کسی class کا صرف ایک instance ہو اور اس تک رسائی کے لیے ایک global نقطہ فراہم کرتا ہے۔ یہ تب استعمال ہوتا ہے جب بالکل ایک object کسی چیز کو coordinate کرے (ایک configuration، connection pool، logger)، اگرچہ یہ کسی حد تک متنازع بھی ہے۔
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
مثال
{
#instance;
() {
(!.#instance) {
.#instance = ();
}
.#instance;
}
}
