Singleton pattern असे सुनिश्चित करते की एका class चा फक्त एक instance असतो आणि त्याला global access point प्रदान करते. हे वापरले जाते जेव्हा नक्की एक object काही गोष्ट समन्वय करणे आवश्यक असे (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;
}
}
