TypeScript में डिज़ाइन पैटर्न: सरल से जटिल तक
\n\nडिज़ाइन पैटर्न (design patterns) सॉफ्टवेयर विकास में सामान्य समस्याओं के समय-परीक्षित समाधान हैं। TypeScript के संदर्भ में, जो JavaScript में सख्त टाइपिंग और OOP क्षमताएँ जोड़ता है, पैटर्न विशेष रूप से प्रभावी हो जाते हैं। TypeScript आपको एक नए स्तर पर एब्स्ट्रैक्शन, इंटरफ़ेस और पॉलीमॉर्फिज़्म को लागू करने की अनुमति देता है, जिससे कोड अधिक पूर्वानुमानित और सुरक्षित हो जाता है।
\n\nइस लेख में, हम प्रमुख डिज़ाइन पैटर्न, TypeScript में उनके कार्यान्वयन और व्यावहारिक उपयोग के उदाहरणों पर चर्चा करेंगे। यह सामग्री शुरुआती डेवलपर्स और अनुभवी पेशेवरों दोनों के लिए उपयोगी होगी जो अपने ज्ञान को व्यवस्थित करना चाहते हैं।
\n\nक्रिएशनल पैटर्न: Singleton और Factory Method
\n\nक्रिएशनल पैटर्न ऑब्जेक्ट बनाने के लिए जिम्मेदार होते हैं, इंस्टेंटिएशन लॉजिक को छिपाते हैं। आइए दो सबसे लोकप्रिय प्रतिनिधियों पर नज़र डालें: Singleton और Factory Method।
\n\nSingleton (सिंगलटन)
\n\nSingleton यह सुनिश्चित करता है कि एक क्लास का केवल एक ही इंस्टेंस हो, और उस तक वैश्विक पहुँच प्रदान करता है। TypeScript में, स्टैटिक मेंबर्स और प्राइवेट कंस्ट्रक्टर के कारण कार्यान्वयन विशेष रूप से सुरुचिपूर्ण हो जाता है।
\n\nclass 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\nFactory Method (फ़ैक्टरी मेथड)
\n\nFactory Method ऑब्जेक्ट बनाने के लिए एक इंटरफ़ेस परिभाषित करता है, लेकिन उपवर्गों को बनाए जाने वाले ऑब्जेक्ट के प्रकार को बदलने की अनुमति देता है। TypeScript में, इसे एब्स्ट्रैक्ट क्लास या इंटरफ़ेस के माध्यम से कार्यान्वित किया जाता है।
\n\ninterface 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\nFactory Method ओपन/क्लोज्ड सिद्धांत (OCP) का पालन करने में मदद करता है — आप मौजूदा कोड को बदले बिना नए प्रकार की सूचनाएँ जोड़ सकते हैं।
\n\nस्ट्रक्चरल पैटर्न: Adapter और Decorator
\n\nस्ट्रक्चरल पैटर्न ऑब्जेक्ट और क्लास के बीच संबंधों को व्यवस्थित करने में मदद करते हैं, जिससे सिस्टम अधिक लचीला और विस्तार योग्य बनता है।
\n\n