Singleton Pattern की जानकारी
In this page:
class Singleton {
private static instance: Singleton;
private constructor() {}
static getInstance(): Singleton {
return (Singleton.instance ??= new Singleton());
}
}
Basic Singleton
एक basic singleton एक static method या property के through किसी class की exactly एक shared instance expose करता है, program का हर part जो इसके लिए पूछे उसे exact वही object वापस मिलना गारंटी देते हुए।
उदाहरण: Basic Singleton
class Config {
private static instance: Config;
static getInstance(): Config {
if (!Config.instance) Config.instance = new Config();
return Config.instance;
}
}
console.log(Config.getInstance() === Config.getInstance());
Private Constructor
एक private constructor वह है जो असल में singleton guarantee enforce करता है — क्योंकि class पर बाहर से new call नहीं किया जा सकता, instance पाने का इकलौता तरीका class के अपने static accessor के through है।
उदाहरण: Private Constructor
class Config {
private static instance: Config;
private constructor() {}
static getInstance(): Config {
if (!Config.instance) Config.instance = new Config();
return Config.instance;
}
}
console.log(Config.getInstance());
Shared State
Shared state singleton का पूरा point है — क्योंकि हर consumer उसी instance का एक reference रखता है, एक reference से किए changes हर दूसरे reference से भी तुरंत दिखते हैं।
उदाहरण: Shared State
class Counter {
private static instance: Counter;
count = 0;
static getInstance(): Counter {
if (!Counter.instance) Counter.instance = new Counter();
return Counter.instance;
}
}
Counter.getInstance().count = 5;
console.log(Counter.getInstance().count);
Lazy Creation
Lazy creation का मतलब है singleton की एक instance सिर्फ तब construct होती है जब इसे असल में request किया जाए, module load होने पर immediately नहीं, जो singleton अनुपयोगी रह जाए तो unnecessary काम से बचाता है।
उदाहरण: Lazy Creation
class Heavy {
private static instance: Heavy;
private constructor() { console.log("Created!"); }
static getInstance(): Heavy {
if (!Heavy.instance) Heavy.instance = new Heavy();
return Heavy.instance;
}
}
console.log("Before first use");
Heavy.getInstance();
Singleton कब उपयोग करें
Singleton सिर्फ तब उपयोग करें जब program-wide सच में एक shared instance चाहिए हो, जैसे एक single configuration object या connection pool — ordinary objects के लिए singletons overuse करना hidden shared state बनाता है जो testing और state के बारे में सोचना कहीं ज़्यादा मुश्किल बना देता है।
उदाहरण: When to Use Singleton
class Logger {
private static instance: Logger;
static getInstance(): Logger {
if (!Logger.instance) Logger.instance = new Logger();
return Logger.instance;
}
log(msg: string) { console.log("[LOG]", msg); }
}
Logger.getInstance().log("App started");
- Constructor को public छोड़ना, इसलिए कोई भी अब भी
new Config()call करके ज़्यादा instances बना सकता है। - हर call पर instance बनाना बजाय पहले
if (!Config.instance)जांचने के। - हर चीज़ के लिए singleton उपयोग करना, जो hidden shared state बनाता है जो testing को मुश्किल बनाता है।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: