परिचय: TypeScript डेवलपर के लिए SOLID क्यों महत्वपूर्ण है
\nSOLID ऑब्जेक्ट-ओरिएंटेड प्रोग्रामिंग के पांच मूलभूत सिद्धांत हैं, जिन्हें रॉबर्ट मार्टिन (अंकल बॉब) ने प्रतिपादित किया था। TypeScript के संदर्भ में, जो स्टैटिक टाइपिंग और JavaScript की लचीलापन को जोड़ता है, SOLID सिद्धांतों का अनुप्रयोग स्केलेबल, परीक्षण योग्य और आसानी से रखरखाव योग्य एप्लिकेशन बनाने में मदद करता है। इस लेख में, हम सैद्धांतिक आधार और TypeScript पर व्यावहारिक उदाहरणों के साथ प्रत्येक सिद्धांत का विश्लेषण करेंगे।
\n\n1. Single Responsibility Principle (SRP) — एकल जिम्मेदारी का सिद्धांत
\nसिद्धांत
\nएक क्लास में बदलाव का केवल एक ही कारण होना चाहिए। इसका मतलब है कि प्रत्येक मॉड्यूल, क्लास या फंक्शन बिल्कुल एक ही कार्यक्षमता के लिए जिम्मेदार होता है।
\n\nTypeScript पर व्यवहार
\nआइए एक क्लास के उदाहरण पर SRP के उल्लंघन को देखें जो उपयोगकर्ताओं और सूचनाओं के भेजने का प्रबंधन करता है:
\n\n// ❌ SRP का उल्लंघन\nclass UserManager {\n constructor(private name: string, private email: string) {}\n\n saveUser(): void {\n console.log(`उपयोगकर्ता ${this.name} डेटाबेस में सहेजा गया`);\n }\n\n sendWelcomeEmail(): void {\n console.log(`स्वागत ईमेल ${this.email} पर भेजा गया`);\n }\n}\n\nजिम्मेदारी के पृथक्करण के साथ सही संस्करण:
\n\n// ✅ SRP का पालन\nclass User {\n constructor(public name: string, public email: string) {}\n}\n\nclass UserRepository {\n save(user: User): void {\n console.log(`उपयोगकर्ता ${user.name} डेटाबेस में सहेजा गया`);\n }\n}\n\nclass EmailService {\n sendWelcomeEmail(user: User): void {\n console.log(`स्वागत ईमेल ${user.email} पर भेजा गया`);\n }\n}\n\n2. Open/Closed Principle (OCP) — खुले/बंद का सिद्धांत
\nसिद्धांत
\nसॉफ्टवेयर संस्थाएं विस्तार के लिए खुली होनी चाहिए, लेकिन संशोधन के लिए बंद होनी चाहिए। दूसरे शब्दों में, आपको मौजूदा कोड को बदले बिना नई कार्यक्षमता जोड़ने में सक्षम होना चाहिए।
\n\nTypeScript पर व्यवहार
\nविभिन्न प्रकार के ग्राहकों के लिए छूट की गणना का उदाहरण:
\n\n// ❌ OCP का उल्लंघन\nclass DiscountCalculator {\n calculateDiscount(customerType: string, amount: number): number {\n if (customerType === 'regular') {\n return amount * 0.1;\n } else if (customerType === 'premium') {\n return amount * 0.2;\n } else if (customerType === 'vip') {\n return amount * 0.3;\n }\n return 0;\n }\n}\n\n// ✅ पॉलीमॉर्फिज्म के माध्यम से OCP का पालन\ninterface DiscountStrategy {\n calculate(amount: number): number;\n}\n\nclass RegularDiscount implements DiscountStrategy {\n calculate(amount: number): number {\n return amount * 0.1;\n }\n}\n\nclass PremiumDiscount implements DiscountStrategy {\n calculate(amount: number): number {\n return amount * 0.2;\n }\n}\n\nclass DiscountCalculator {\n constructor(private strategy: DiscountStrategy) {}\n\n calculate(amount: number): number {\n return this.strategy.calculate(amount);\n }\n}\n\n3. Liskov Substitution Principle (LSP) — बारबरा लिस्कोव का प्रतिस्थापन सिद्धांत
\nसिद्धांत
\nप्रोग्राम में ऑब्जेक्ट्स को प्रोग्राम की शुद्धता को बदले बिना उनके उपप्रकारों के उदाहरणों द्वारा प्रतिस्थापित किया जाना चाहिए। सरल शब्दों में: यदि आपके पास एक बेस क्लास है, तो उसका कोई भी वंशज हर जगह सही ढंग से काम करना चाहिए जहां बेस क्लास का उपयोग किया जाता है।
\n\nTypeScript पर व्यवहार
\nज्यामितीय आकृतियों के साथ क्लासिक उदाहरण:
\n\n// ❌ LSP का उल्लंघन\nclass Rectangle {\n constructor(protected width: number, protected height: number) {}\n\n setWidth(width: number): void {\n this.width = width;\n }\n\n setHeight(height: number): void {\n this.height = height;\n }\n\n getArea(): number {\n return this.width * this.height;\n }\n}\n\nclass Square extends Rectangle {\n setWidth(width: number): void {\n this.width = width;\n this.height = width; // उल्लंघन!\n }\n\n setHeight(height: number): vo