TypeScript में डिज़ाइन पैटर्न उदाहरण सहित: A से Z तक

शुरुआती लोगों के लिए ऑनलाइन Python प्रशिक्षक

Python आसानी से सीखें बिना सिद्धांत के बोझ के। स्वचालित जांच के साथ व्यावहारिक कार्य हल करें और ब्राउज़र में सीधे कोड लिखें।

पाठ्यक्रम शुरू करें

TypeScript में डिज़ाइन पैटर्न: सरल से जटिल तक

\n\n

डिज़ाइन पैटर्न (design patterns) सॉफ्टवेयर विकास में सामान्य समस्याओं के समय-परीक्षित समाधान हैं। TypeScript के संदर्भ में, जो JavaScript में सख्त टाइपिंग और OOP क्षमताएँ जोड़ता है, पैटर्न विशेष रूप से प्रभावी हो जाते हैं। TypeScript आपको एक नए स्तर पर एब्स्ट्रैक्शन, इंटरफ़ेस और पॉलीमॉर्फिज़्म को लागू करने की अनुमति देता है, जिससे कोड अधिक पूर्वानुमानित और सुरक्षित हो जाता है।

\n\n

इस लेख में, हम प्रमुख डिज़ाइन पैटर्न, TypeScript में उनके कार्यान्वयन और व्यावहारिक उपयोग के उदाहरणों पर चर्चा करेंगे। यह सामग्री शुरुआती डेवलपर्स और अनुभवी पेशेवरों दोनों के लिए उपयोगी होगी जो अपने ज्ञान को व्यवस्थित करना चाहते हैं।

\n\n

क्रिएशनल पैटर्न: Singleton और Factory Method

\n\n

क्रिएशनल पैटर्न ऑब्जेक्ट बनाने के लिए जिम्मेदार होते हैं, इंस्टेंटिएशन लॉजिक को छिपाते हैं। आइए दो सबसे लोकप्रिय प्रतिनिधियों पर नज़र डालें: Singleton और Factory Method।

\n\n

Singleton (सिंगलटन)

\n\n

Singleton यह सुनिश्चित करता है कि एक क्लास का केवल एक ही इंस्टेंस हो, और उस तक वैश्विक पहुँच प्रदान करता है। TypeScript में, स्टैटिक मेंबर्स और प्राइवेट कंस्ट्रक्टर के कारण कार्यान्वयन विशेष रूप से सुरुचिपूर्ण हो जाता है।

\n\n
class DatabaseConnection {\n    private static instance: DatabaseConnection;\n    private connectionString: string;\n\n    private constructor(connectionString: string) {\n        this.connectionString = connectionString;\n        console.log(`इससे कनेक्शन बनाया गया: ${connectionString}`);\n    }\n\n    static getInstance(connectionString: string): DatabaseConnection {\n        if (!DatabaseConnection.instance) {\n            DatabaseConnection.instance = new DatabaseConnection(connectionString);\n        }\n        return DatabaseConnection.instance;\n    }\n\n    query(sql: string): void {\n        console.log(`क्वेरी निष्पादित कर रहा है: ${sql} पर ${this.connectionString}`);\n    }\n}\n\n// उपयोग\nconst db1 = DatabaseConnection.getInstance('postgres://localhost:5432/mydb');\nconst db2 = DatabaseConnection.getInstance('postgres://localhost:5432/mydb');\nconsole.log(db1 === db2); // true - एक ही ऑब्जेक्ट
\n\n

यह पैटर्न संसाधन प्रबंधन के लिए आदर्श है: कनेक्शन पूल, लॉगिंग, कैशिंग। हालाँकि, सावधान रहें — Singleton वैश्विक स्थिति के कारण परीक्षण को जटिल बना सकता है।

\n\n

Factory Method (फ़ैक्टरी मेथड)

\n\n

Factory Method ऑब्जेक्ट बनाने के लिए एक इंटरफ़ेस परिभाषित करता है, लेकिन उपवर्गों को बनाए जाने वाले ऑब्जेक्ट के प्रकार को बदलने की अनुमति देता है। TypeScript में, इसे एब्स्ट्रैक्ट क्लास या इंटरफ़ेस के माध्यम से कार्यान्वित किया जाता है।

\n\n
interface Notification {\n    send(message: string): void;\n}\n\nclass EmailNotification implements Notification {\n    send(message: string): void {\n        console.log(`ईमेल भेज रहा है: ${message}`);\n    }\n}\n\nclass SMSNotification implements Notification {\n    send(message: string): void {\n        console.log(`SMS भेज रहा है: ${message}`);\n    }\n}\n\nabstract class NotificationFactory {\n    abstract createNotification(): Notification;\n\n    notify(message: string): void {\n        const notification = this.createNotification();\n        notification.send(message);\n    }\n}\n\nclass EmailFactory extends NotificationFactory {\n    createNotification(): Notification {\n        return new EmailNotification();\n    }\n}\n\nclass SMSFactory extends NotificationFactory {\n    createNotification(): Notification {\n        return new SMSNotification();\n    }\n}\n\n// उपयोग\nconst factory: NotificationFactory = new EmailFactory();\nfactory.notify('आपका ऑर्डर कन्फर्म हो गया है!');
\n\n

Factory Method ओपन/क्लोज्ड सिद्धांत (OCP) का पालन करने में मदद करता है — आप मौजूदा कोड को बदले बिना नए प्रकार की सूचनाएँ जोड़ सकते हैं।

\n\n

स्ट्रक्चरल पैटर्न: Adapter और Decorator

\n\n

स्ट्रक्चरल पैटर्न ऑब्जेक्ट और क्लास के बीच संबंधों को व्यवस्थित करने में मदद करते हैं, जिससे सिस्टम अधिक लचीला और विस्तार योग्य बनता है।

\n\n

Adapt

ब्लॉग

पुस्तक अनुशंसाएँ