Singleton প্যাটার্ন নিশ্চিত করে যে একটি ক্লাসের শুধুমাত্র একটি instance থাকে এবং এটি একটি গ্লোবাল অ্যাক্সেস পয়েন্ট সরবরাহ করে। এটি ব্যবহৃত হয় যখন ঠিক একটি অবজেক্ট কোনো কিছু সমন্বয় করবে (কনফিগারেশন, কানেকশন পুল, 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;
}
}
