← Back to TypeScript Course | Chapter 16: Advanced Patterns | Lesson 3 of 7

Singleton Pattern की जानकारी

Singleton Pattern गारंटी देता है कि एक class का सिर्फ एक shared instance हो। यह उपयोगी है जब एक centralized object जानबूझकर पूरी application में shared हो।
Syntax
typescript
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

typescript
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

typescript
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

typescript
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

typescript
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

typescript
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");
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. Constructor को public छोड़ना, इसलिए कोई भी अब भी new Config() call करके ज़्यादा instances बना सकता है।
  2. हर call पर instance बनाना बजाय पहले if (!Config.instance) जांचने के।
  3. हर चीज़ के लिए singleton उपयोग करना, जो hidden shared state बनाता है जो testing को मुश्किल बनाता है।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.