TypeScript में डिज़ाइन पैटर्न: व्यावहारिक मार्गदर्शिका
डिज़ाइन पैटर्न (design patterns) सॉफ्टवेयर विकास में सामान्य समस्याओं के सिद्ध समाधान हैं। TypeScript में, सख्त टाइपिंग और OOP क्षमताओं के कारण, कई पैटर्न विशेष रूप से सुरुचिपूर्ण और सुरक्षित रूप से कार्यान्वित होते हैं। इस लेख में, हम वास्तविक कोड उदाहरणों के साथ चार प्रमुख पैटर्न का विश्लेषण करेंगे जो आपको अधिक लचीला और रखरखाव योग्य कोड लिखने में मदद करेंगे।
1. Singleton (एकल)
Singleton पैटर्न सुनिश्चित करता है कि एक वर्ग का केवल एक उदाहरण मौजूद है, और उस तक वैश्विक पहुँच प्रदान करता है। TypeScript में, यह एप्लिकेशन कॉन्फ़िगरेशन या डेटाबेस कनेक्शन प्रबंधित करने के लिए विशेष रूप से उपयोगी है।
प्राइवेट कंस्ट्रक्टर और स्टैटिक विधि का उपयोग करके कार्यान्वयन:
class DatabaseConnection { private static instance: DatabaseConnection; private connection: string;
private constructor() { this.connection = 'PostgreSQL connected'; }
public static getInstance(): DatabaseConnection { if (!DatabaseConnection.instance) { DatabaseConnection.instance = new DatabaseConnection(); } return DatabaseConnection.instance; }
public query(sql: string): void { console.log(`Executing query: ${sql} on ${this.connection}`); }}
// उपयोगconst db1 = DatabaseConnection.getInstance();const db2 = DatabaseConnection.getInstance();console.log(db1 === db2); // true - एक ही ऑब्जेक्टdb1.query('SELECT * FROM users');कब उपयोग करें: लॉगर्स, कैश, थ्रेड पूल, एप्लिकेशन सेटिंग्स के लिए। मल्टी-थ्रेडेड वातावरण में बदलने वाली स्थितियों के लिए Singleton से बचें।
2. Factory Method (फ़ैक्टरी विधि)
Factory Method ऑब्जेक्ट बनाने के लिए एक इंटरफ़ेस परिभाषित करता है, लेकिन उपवर्गों को बनाए जाने वाले ऑब्जेक्ट के प्रकार को बदलने की अनुमति देता है। TypeScript में, यह इंटरफ़ेस और एब्सट्रैक्ट क्लास के साथ अच्छी तरह से मेल खाता है।
विभिन्न प्रकार की सूचनाएँ बनाने का उदाहरण:
interface Notification { send(message: string): void;}
class EmailNotification implements Notification { public send(message: string): void { console.log(`Email sent: ${message}`); }}
class SMSNotification implements Notification { public send(message: string): void { console.log(`SMS sent: ${message}`); }}
abstract class NotificationFactory { public abstract createNotification(): Notification;
public notifyUser(message: string): void { const notification = this.createNotification(); notification.send(message); }}
class EmailNotificationFactory extends NotificationFactory { public createNotification(): Notification { return new EmailNotification(); }}
class SMSNotificationFactory extends NotificationFactory { public createNotification(): Notification { return new SMSNotification(); }}
// उपयोगconst factory: NotificationFactory = new EmailNotificationFactory();factory.notifyUser('Your order has been shipped!');कब उपयोग करें: जब पहले से पता न हो कि किस विशिष्ट वर्ग की आवश्यकता होगी, या जब ऑब्जेक्ट निर्माण तर्क जटिल हो और केंद्रीकृत होना चाहिए।
3. Observer (प्रेक्षक)
Observer ऑब्जेक्ट्स के बीच "एक-से-अनेक" संबंध परिभाषित करता है: जब एक ऑब्जेक्ट की स्थिति बदलती है, तो सभी आश्रित ऑब्जेक्ट स्वचालित रूप से सूचित होते हैं। TypeScript में, इसे अक्सर इवेंट्स को टाइप करने के लिए जेनेरिक के माध्यम से कार्यान्वित किया जाता है।
समाचार सदस्यता प्रणाली का उदाहरण:
interface Observer<T> { update(data: T): void;}
class NewsPublisher<T> { private observers: Observer<T>[] = [];
public subscribe(observer: Observer<T>): void { this.observers.push(observer); }
public unsubscribe(observer: Observer<T>): void { this.observers = this.observers.filter(obs => o