December 20, 2025 (11mo ago) — last updated November 4, 2026 (14d ago)

TypeScript OOP Design Patterns: Practical Guide

Creational, structural और behavioral OOP पैटर्न TypeScript उदाहरणों के साथ सीखें — बेहतर मेंटेनिबिलिटी, टेस्टेबिलिटी और साफ़ आर्किटेक्चर के लिए।

← Back to blog
Cover Image for TypeScript OOP Design Patterns: Practical Guide

ऑब्जेक्ट-ओरिएंटेड डिज़ाइन पैटर्न परखे हुए, पुन: उपयोग योग्य समाधान हैं जो क्लास और ऑब्जेक्ट संरचना को इस तरह रूप देते हैं कि कोड बनाना, बढ़ाना और टेस्ट करना आसान रहे। इस गाइड में creational, structural और behavioral पैटर्न्स को व्यावहारिक TypeScript उदाहरणों के साथ समझाया गया है ताकि आप रियल-वर्ल्ड प्रॉब्लम्स के लिए सही पैटर्न चुन सकें। Gang of Four ने 23 मूल पैटर्न सूचीबद्ध किए हैं1 और TypeScript डेवलपर समुदाय में व्यापक रूप से इस्तेमाल हो रही है5।

TypeScript OOP Design Patterns: Practical Guide

Master OOP design patterns with clear, practical TypeScript examples. Learn creational, structural, and behavioral patterns to improve maintainability and design clarity.

Introduction

ऑब्जेक्ट-ओरिएंटेड डिज़ाइन पैटर्न बार-बार आने वाली डिज़ाइन चुनौतियों के लिए परखा हुआ, पुन: उपयोग योग्य टेम्पलेट हैं। ये सीधे कॉपी-पेस्ट करने के लिए कोड नहीं होते; वे आपकी क्लास और ऑब्जेक्ट संरचना को इस तरह रूप देते हैं कि कोड बनाना, बढ़ाना और टेस्ट करना आसान रहे। इस गाइड में creational, structural और behavioral पैटर्न्स को व्यावहारिक TypeScript उदाहरणों के साथ समझाया गया है ताकि आप रियल-वर्ल्ड प्रॉब्लम्स के लिए सही पैटर्न चुन सकें। Gang of Four ने 23 मूल पैटर्न सूचीबद्ध किए जो आज भी व्यापक रूप से उपयोग होते हैं1। TypeScript जैसी भाषाएँ पैटर्न की स्पष्टता और टाइप-सुरक्षा दोनों देती हैं, और यह वेब-डेवलपर समुदाय में व्यापक रूप से इस्तेमाल हो रही है5。

क्या हैं OOP डिज़ाइन पैटर्न?

डिज़ाइन पैटर्न परखे हुए समाधान हैं जो अच्छी डिजाइन प्रैक्टिसेज़ को कैप्चर करते हैं और टीमों के बीच साझा शब्दावली बनाते हैं। पैटर्न न केवल कोड की संरचना बताते हैं, बल्कि डिज़ाइन के इरादे को भी स्पष्ट करते हैं — जिससे टीम में संवाद तेज़ और निर्णय बेहतर होते हैं। कई अध्ययनों ने पैटर्न्स के अमल से मिलने वाले लाभों जैसे बेहतर रीयूज़ेबिलिटी और मेंटेनेंस पर प्रकाश डाला है2。

एक शेफ़ की टोपी का स्केच जो सुनहरे अक्षर के आकार को एक खुले कुकबुक में गिरा रहा है।

डेवलपर्स के बीच साझा शब्दावली

“Factory”, “Singleton”, या “Observer” जैसे नाम सुनते ही अनुभवी डेवलपर तुरंत इरादा समझ जाते हैं—यह साझा शब्दावली डिज़ाइन चर्चा को तेज़ और स्पष्ट बनाती है। कोर OOP सिद्धांत—पॉलिमॉर्फिज़्म, इनहेरिटेंस और कंपोजिशन—पैटर्न को प्रभावी ढंग से लागू करने की नींव हैं। अगर आपको बेसिक OOP रिफ्रेशर चाहिए, तो यह तुलना उपयोगी है: Polymorphism vs Inheritance.

तीन मुख्य श्रेणियाँ: Creational, Structural, Behavioral

Gang of Four ने पैटर्न्स को तीन श्रेणियों में बाँटा: creational, structural और behavioral। इस ग्रुपिंग से आप किसी समस्या के लिए तेज़ और उपयुक्त पैटर्न चुन सकते हैं1。

creational, structural, and behavioural design patterns को सरल बॉक्स इलस्ट्रेशंस के साथ दर्शाता हुआ डायग्राम।

अवलोकन

Pattern CategoryCore PurposeCommon Examples
CreationalManage and abstract object creationFactory, Builder, Singleton, Prototype
StructuralCompose classes and objects into larger, flexible structuresAdapter, Decorator, Facade, Composite
BehavioralDefine how objects interact and communicateObserver, Strategy, Command, Iterator

Creational Patterns — निर्माण की नीतियां

Creational पैटर्न्स नियंत्रित करते हैं कि ऑब्जेक्ट्स कैसे बनाए जाते हैं, ताकि क्लाइंट कोड concrete क्लासेज़ से tightly coupled न हो। ये instantiation लॉजिक को छिपाते हैं और implementations को बिना क्लाइंट बदलने के स्वैप करने की सुविधा देते हैं।

Singleton — एकमात्र उदाहरण सुनिश्चित करना

Singleton सुनिश्चित करता है कि किसी क्लास का केवल एक ही उदाहरण हो और वह एक वैश्विक एक्सेस प्वाइंट प्रदान करे। डेटाबेस कनेक्शन या लॉगर जैसे साझा संसाधनों के लिए यह उपयोगी है, लेकिन इसके परीक्षण और डिपेंडेंसी मैनेजमेंट पर प्रभावों का ध्यान रखें3。

Example: a TypeScript Singleton for a database connection.

class DatabaseConnection {
  private static instance: DatabaseConnection;

  private constructor() {
    // A private constructor prevents external "new" calls
    console.log("Connecting to the database...");
  }

  public static getInstance(): DatabaseConnection {
    if (!DatabaseConnection.instance) {
      DatabaseConnection.instance = new DatabaseConnection();
    }
    return DatabaseConnection.instance;
  }

  public query(sql: string): void {
    console.log(`Executing query: ${sql}`);
  }
}

// Usage
const db1 = DatabaseConnection.getInstance();
const db2 = DatabaseConnection.getInstance();

db1.query("SELECT * FROM users");
console.log(db1 === db2); // true

Factory Method — कौन से ऑब्जेक्ट बनेंगे, यह सबक्लास तय करे

Factory Method एक ऑब्जेक्ट बनाने के लिए एक इंटरफ़ेस परिभाषित करता है, जबकि सबक्लासेस तय करती हैं कि किस concrete प्रोडक्ट को instantiate करना है। यह क्लाइंट कोड को concrete क्लासेज़ से decouple करता है और नए प्रोडक्ट जोड़ना सरल बनाता है।

Example: rendering OS-specific buttons in TypeScript.

interface Button {
  render(): void;
  onClick(f: () => void): void;
}

class WindowsButton implements Button {
  render() { console.log("Rendering a button in Windows style."); }
  onClick(f: () => void) { console.log("Windows button click event."); f(); }
}

class MacButton implements Button {
  render() { console.log("Rendering a button in macOS style."); }
  onClick(f: () => void) { console.log("Mac button click event."); f(); }
}

abstract class Dialog {
  abstract createButton(): Button;

  render() {
    const okButton = this.createButton();
    okButton.render();
  }
}

class WindowsDialog extends Dialog {
  createButton(): Button { return new WindowsButton(); }
}

class MacDialog extends Dialog {
  createButton(): Button { return new MacButton(); }
}

// Client
const os: string = "windows";
let dialog: Dialog;
if (os === "windows") dialog = new WindowsDialog(); else dialog = new MacDialog();
dialog.render();

Structural Patterns — वास्तुशिल्पीय जोड़

Structural पैटर्न्स सिस्टम को बड़े, अनुकूलनीय हिस्सों में जोड़ने में मदद करते हैं। वे इंटरफेसों और क्लासेज़ के बीच रिश्तों को सरल करते हैं ताकि आप बिना बड़े बदलाव के सिस्टम का विस्तार कर सकें।

एक इलेक्ट्रिकल एडॉप्टर और रिंग्स के स्टैक का हैंड-ड्रॉन इलस्ट्रेशन जो OOP डिज़ाइन पैटर्न्स का प्रतिनिधित्व करता है।

Adapter — असंगत इंटरफेसों का पुल

Adapter किसी असंगत इंटरफ़ेस को रैप करके उसे उस रूप में ढाल देता है जिसकी आपकी सिस्टम को उम्मीद होती है। यह लेगेसी कोड में invasive बदलाव करने से बचाता है।

Example: adapting a ModernLogger to an existing ILogger interface.

class ModernLogger {
  public logInfo(message: string): void {
    console.log(`[INFO]: ${message}`);
  }
}

interface ILogger { log(message: string): void; }

class LoggerAdapter implements ILogger {
  private modernLogger: ModernLogger;
  constructor() { this.modernLogger = new ModernLogger(); }
  public log(message: string): void { this.modernLogger.logInfo(message); }
}

const logger: ILogger = new LoggerAdapter();
logger.log("User logged in successfully.");

Decorator — रनटाइम पर कार्यक्षमता जोड़ें

Decorator runtime में ऑब्जेक्ट्स को रैप करके उन पर कार्यक्षमता जोड़ता है। यह subclassing की तुलना में अधिक लचीला है और Single Responsibility Principle का पालन करने में मदद करता है।

Example: composing a subscription with optional add-ons.

interface Subscription { getDescription(): string; getCost(): number; }

class BasicSubscription implements Subscription {
  getDescription(): string { return "Basic Plan"; }
  getCost(): number { return 10; }
}

abstract class SubscriptionDecorator implements Subscription {
  protected subscription: Subscription;
  constructor(subscription: Subscription) { this.subscription = subscription; }
  abstract getDescription(): string;
  abstract getCost(): number;
}

class PremiumSupportDecorator extends SubscriptionDecorator {
  getDescription(): string { return `${this.subscription.getDescription()}, Premium Support`; }
  getCost(): number { return this.subscription.getCost() + 5; }
}

class CloudStorageDecorator extends SubscriptionDecorator {
  getDescription(): string { return `${this.subscription.getDescription()}, 1TB Cloud Storage`; }
  getCost(): number { return this.subscription.getCost() + 7; }
}

let mySubscription: Subscription = new BasicSubscription();
mySubscription = new PremiumSupportDecorator(mySubscription);
mySubscription = new CloudStorageDecorator(mySubscription);
console.log(mySubscription.getDescription());
console.log(mySubscription.getCost());

Behavioral Patterns — इंटरैक्शन का प्रबंधन

Behavioral पैटर्न्स ऑब्जेक्ट्स के बीच इंटरैक्शन को व्यवस्थित करते हैं ताकि सिस्टम लचीला और मेंटेन करने योग्य रहे। वे कम्युनिकेशन पाथ्स को साफ़ करते हैं और जिम्मेदारियों को स्पष्ट करते हैं।

Observer — रुचि रखने वालों को सूचित करना

Observer एक one-to-many संबंध बनाता है: जब Subject बदलता है, Observers को सूचित किया जाता है। यह ईवेंट-ड्रिवन सिस्टम्स के लिए आदर्श है।

Example: a simple notification service.

interface Subject { attach(observer: Observer): void; detach(observer: Observer): void; notify(): void; }
interface Observer { update(subject: Subject): void; }

class NotificationService implements Subject {
  public state: string = '';
  private observers: Observer[] = [];
  attach(observer: Observer): void { this.observers.push(observer); }
  detach(observer: Observer): void { const i = this.observers.indexOf(observer); if (i !== -1) this.observers.splice(i, 1); }
  notify(): void { for (const o of this.observers) o.update(this); }
  public createNewPost(title: string): void { this.state = `New Post: ${title}`; console.log("\nNotificationService: A new post was created."); this.notify(); }
}

class EmailNotifier implements Observer { public update(subject: Subject): void { if (subject instanceof NotificationService) console.log(`EmailNotifier: Sending email about "${subject.state}"`); } }
class PushNotifier implements Observer { public update(subject: Subject): void { if (subject instanceof NotificationService) console.log(`PushNotifier: Sending push notification for "${subject.state}"`); } }

const notificationService = new NotificationService();
const emailer = new EmailNotifier();
const pusher = new PushNotifier();
notificationService.attach(emailer);
notificationService.attach(pusher);
notificationService.createNewPost("Understanding Observer Pattern");
notificationService.detach(pusher);
notificationService.createNewPost("Why Strategy is Awesome");

Observer एक loosely coupled डिज़ाइन पैदा करता है: Subjects को concrete Observers के बारे में पता होना जरूरी नहीं।

Strategy — एल्गोरिदम को कैप्सुलेट करना

Strategy runtime पर एल्गोरिदम स्वैप करने देता है, जिससे बड़े conditional ब्लॉक्स से बचा जा सकता है और Open/Closed Principle का पालन आसान हो जाता है4。

Example: payment strategies for a shopping cart.

interface PaymentStrategy { pay(amount: number): void; }
class CreditCardStrategy implements PaymentStrategy { pay(amount: number): void { console.log(`Paying $${amount} with Credit Card.`); } }
class PayPalStrategy implements PaymentStrategy { pay(amount: number): void { console.log(`Paying $${amount} via PayPal.`); } }

class ShoppingCart {
  private paymentStrategy: PaymentStrategy;
  constructor(strategy: PaymentStrategy) { this.paymentStrategy = strategy; }
  public setPaymentStrategy(strategy: PaymentStrategy) { this.paymentStrategy = strategy; }
  public checkout(amount: number): void { this.paymentStrategy.pay(amount); }
}

const cart = new ShoppingCart(new CreditCardStrategy());
cart.checkout(150);
cart.setPaymentStrategy(new PayPalStrategy());
cart.checkout(150);

सामान्य डिज़ाइन गलतियाँ और रिफैक्टरिंग

किसी पैटर्न का चयन करना केवल आधी लड़ाई है। God Object जैसे एंटी-पैटर्न से बचें, जो जिम्मेदारियों को इकट्ठा कर लेता है और Single Responsibility Principle का उल्लंघन करता है। कोड स्मेल—लंबे मेथड्स, अत्यधिक डिपेंडेंसीज़, या विशाल क्लासेज़—पर ध्यान दें और अनुशासित रिफैक्टरिंग से उन्हें ठीक करें। रिफैक्टरिंग का मतलब है बाहरी व्यवहार बदले बिना आंतरिक संरचना में सुधार करना; यही तरीका लेगेसी सिस्टम्स को सुरक्षित रूप से सुधारने का है।

Refactoring to Strategy

बड़ा switch या लंबी if/else चेन एक क्लासिक स्मेल है। अलग व्यवहार को Strategy इंटरफ़ेस में निकालकर और concrete strategy क्लासेज़ बनाकर रिफैक्टर करें। इससे एक्स्टेंसिबिलिटी, टेस्टेबिलिटी और SOLID सिद्धांतों का पालन आसान होता है4。


Practical Recommendations

  • पहले 5–7 प्रमुख पैटर्न्स पर महारत हासिल करें: Singleton, Factory, Adapter, Decorator, Observer और Strategy।
  • पैटर्न तभी लागू करें जब वह समस्या का स्पष्ट समाधान दे—अतिरिक्त जटिलता से बचें।
  • Functional शैली में भी पैटर्न्स के सिद्धांत लागू होते हैं; उदाहरण के लिए Strategy एक फ़ंक्शन पैरामीटर हो सकता है।
  • TypeScript जैसी आधुनिक भाषाएँ पैटर्न की स्पष्टता और टाइप-सुरक्षा दोनों देती हैं; TypeScript डेवलपर समुदाय में व्यापक रूप से इस्तेमाल होती है5。

Frequently Asked Questions

Q: कौन से पेहलकदम पैटर्न पहले सीखने चाहिए?

A: शुरुआत में Singleton, Factory, Adapter, Decorator, Observer और Strategy पर फोकस करें—ये रोज़मर्रा की समस्याओं का समाधान देने के लिए सबसे उपयोगी हैं।

Q: कब पैटर्न का उपयोग करने से बचना चाहिए?

A: अगर पैटर्न केवल जटिलता जोड़ता है और स्पष्ट लाभ नहीं देता, तो उसे न लगाएँ। YAGNI और KISS विचार रखें।

Q: क्या फंक्शनल कोडबेस में पैटर्न काम करते हैं?

A: हाँ। कई पैटर्न्स फंक्शनल तकनीकों से अच्छी तरह मैप होते हैं—Strategy फ़ंक्शन पैरामीटर हो सकता है और Decorator higher-order फ़ंक्शंस हो सकते हैं।


At Clean Code Guy, we help teams build software that lasts by embedding foundational principles into their workflow. Discover how our code audits and AI-ready refactoring can get your team shipping with confidence at https://cleancodeguy.com.

1.
Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, Design Patterns: Elements of Reusable Object-Oriented Software (Addison-Wesley, 1994). https://en.wikipedia.org/wiki/Design_Patterns
2.
W. G. Griswold et al., “A Formal Foundation for Design Pattern Abstraction and Reuse,” Proceedings of ECOOP ’93. https://cseweb.ucsd.edu/~wgg/CSE210/ecoop93-patterns.pdf
3.
Martin Fowler, “Singleton,” Bliki. https://martinfowler.com/bliki/Singleton.html
4.
Robert C. Martin (Uncle Bob), “The SOLID Principles.” https://8thlight.com/blog/uncle-bob/2012/08/13/the-s-o-l-i-d-principles.html
5.
Stack Overflow, Developer Survey 2023, shows TypeScript among the most-used languages in modern web development. https://insights.stackoverflow.com/survey/2023
← Back to blog
🙋🏻‍♂️

AI कोड लिखता है।
आप इसे टिकाऊ बनाते हैं।

AI त्वरण के युग में, क्लीन कोड केवल एक अच्छी प्रथा नहीं है — यह उन प्रणालियों के बीच का अंतर है जो स्केल होती हैं और कोडबेस जो अपने वजन के तहत ढह जाते हैं।

TypeScript OOP Design Patterns: Practical Guide | Clean Code Guy