जावा में डिज़ाइन पैटर्न कोड उदाहरणों के साथ

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

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

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

जावा में डिज़ाइन पैटर्न: उदाहरणों के साथ पूर्ण गाइड



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



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



क्रिएशनल पैटर्न (Creational Patterns)



क्रिएशनल पैटर्न ऑब्जेक्ट निर्माण की प्रक्रिया को प्रबंधित करते हैं, सिस्टम को उनके निर्माण के तरीके से स्वतंत्र बनाते हैं। आइए दो सबसे लोकप्रिय टेम्पलेट देखें: Singleton और Factory Method।



Singleton (एकल)



Singleton यह सुनिश्चित करता है कि एक क्लास का केवल एक इंस्टेंस हो, और उस तक वैश्विक पहुँच प्रदान करता है। इस पैटर्न का उपयोग अक्सर डेटाबेस कनेक्शन, लॉगिंग या कैशिंग के लिए किया जाता है।



जावा में थ्रेड-सेफ Singleton का कार्यान्वयन उदाहरण:



public class DatabaseConnection {    private static volatile DatabaseConnection instance;    private String connectionString;

private DatabaseConnection(String connectionString) { this.connectionString = connectionString; }

public static DatabaseConnection getInstance(String connectionString) { if (instance == null) { synchronized (DatabaseConnection.class) { if (instance == null) { instance = new DatabaseConnection(connectionString); } } } return instance; }

public void connect() { System.out.println("कनेक्ट हो रहा है " + connectionString); }}


उपयोग:



DatabaseConnection db = DatabaseConnection.getInstance("jdbc:mysql://localhost:3306/mydb");db.connect();


Factory Method (फैक्टरी विधि)



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



विभिन्न प्रकार की सूचनाएँ बनाने के लिए फैक्टरी विधि का उदाहरण:



interface Notification {    void send(String message);}

class EmailNotification implements Notification { public void send(String message) { System.out.println("ईमेल भेज रहा है: " + message); }}

class SMSNotification implements Notification { public void send(String message) { System.out.println("SMS भेज रहा है: " + message); }}

abstract class NotificationFactory { public abstract Notification createNotification();

public void notifyUser(String message) { Notification notification = createNotification(); notification.send(message); }}

class EmailNotificationFactory extends NotificationFactory { public Notification createNotification() { return new EmailNotification(); }}

class SMSNotificationFactory extends NotificationFactory { public Notification createNotification() { return new SMSNotification(); }}


उपयोग:



NotificationFactory factory = new EmailNotificationFactory();factory.notifyUser("आपका ऑर्डर कन्फर्म हो गया है!");


स्ट्रक्चरल पैटर्न (Structural Patterns)



स्ट्रक्चरल पैटर्न बड़ी संरचनाएँ बनाने के लिए क्लासेस और ऑब्जेक्ट्स को संयोजित करने के तरीकों का वर्णन करते हैं। आइए Adapter (अनुकूलक) पैटर्न देखें।



Adapter (अनुकूलक)



Adapter असंगत इंटरफ़ेस वाले ऑब्जेक्ट्स को एक साथ काम करने की अनुमति देता है। यह एक ऑब्जेक्ट को लपेटता है, क्लाइंट के लिए आवश्यक इंटरफ़ेस प्रदान करता है।



पुरानी लॉगिंग प्रणाली को एकीकृत करने के लिए एडाप्टर का उदाहरण

ब्लॉग

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