Singleton, Factory, Abstract Factory Java In Hindi
जावा प्रोग्रामिंग लैंग्वेज में, सिंगलटन पैटर्न, फ़ैक्टरी पैटर्न, और एब्स्ट्रैक्ट फ़ैक्टरी पैटर्न, ऑब्जेक्ट-ओरिएंटेड प्रोग्रामिंग एनवायरनमेंट में एक क्रिएशनल डिज़ाइन पैटर्न मेथड हैं। ये जावा डेवलपर को एक नए क्लास ऑब्जेक्ट जावा में कैसे क्रिएट किये जाते हैं, उन्हें कंट्रोल और मैनेज करने में हेल्प करते हैं।

Singleton pattern concept in Java oops.
जावा क्लास प्रोग्राम में सिंगलटन पैटर्न यह मैनेज करने में हेल्प करता है कि पूरे यूजर डिफाइन एप्लिकेशन में एक क्लास का सिर्फ़ एक ऑब्जेक्ट (इंस्टेंस) क्रिएटेड हो और उन्हें एक्सेस करने का एक ग्लोबल मेथड प्रोवाइड करता है।
Instances for database connections, loggers, and configuration managers.
Example of Singleton pattern concept in Java.
class Singleton {
private static Singleton instance;
// here we create a private constructor method
private Singleton() {
}
// here we used public method to get the single instance
public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
public class Main {
public static void main(String[] args) {
Singleton sample1 = Singleton.getInstance();
Singleton sample2 = Singleton.getInstance();
System.out.println(sample1 == sample2);
}
}
Here’s the Singleton pattern main idea in this example.
एक सिंगलटन पैटर्न क्लास → एक सैंपल ऑब्जेक्ट क्रिएट करता है.
Factory pattern concept in Java oops.
जावा क्लास प्रोग्राम में फ़ैक्टरी पैटर्न जावा क्लाइंट यूजर को क्लास ऑब्जेक्ट-क्रिएशन लॉजिक एक्सप्रेशन मेथड को डिस्प्ले किये बिना क्लास ऑब्जेक्ट को क्रिएट करता है।
जैसे, जावा यूजर फैक्ट्री पैटर्न में डायरेक्ट अलग-अलग तरह के Company क्रिएट करने के बदले, एक फैक्ट्री मेथड से एक Employee आर्गेनाइजेशन को क्रिएट करने के लिए कहते हैं।
Example of Factory pattern concept in Java.
interface Company {
void dept();
}
class Employee implements Company {
@Override
public void dept() {
System.out.println(“Department of Employee”);
}
}
class Manager implements Company {
@Override
public void dept() {
System.out.println(“Department of Manager”);
}
}
class CompanyFactory {
public static Company createCompany(String type) {
if (type.equalsIgnoreCase(“Employee”)) {
return new Employee();
}
else if (type.equalsIgnoreCase(“Manager”)) {
return new Manager();
}
return null;
}
}
public class Main {
public static void main(String[] args) {
Company sample1 = CompanyFactory.createCompany(“Employee”);
Company sample2 = CompanyFactory.createCompany(“Manager”);
sample1.dept();
sample2.dept();
}
}
Used in the Java Factory pattern.
Company c = CompanyFactory.createCompany(“employee”);
c.dept();
Here’s the Factory pattern main idea in this example.
एक जावा फ़ैक्टरी पैटर्न → यूजर डिफाइन क्लास में अलग-अलग तरह के रिलेटेड क्लास ऑब्जेक्ट क्रिएट करता है
Abstract Factory pattern concept in Java oops.
जावा क्लास प्रोग्राम में एब्स्ट्रैक्ट फ़ैक्टरी क्लास पैटर्न में कई फ़ैक्टरियों की एक फ़ैक्टरी मेथड है। जावा प्रोग्राम में एब्स्ट्रैक्ट फ़ैक्टरी क्लास पैटर्न कांसेप्ट का यूज़ तब किया जाता है. जब जावा डेवलपर को किसी रिलेटेड क्लास ऑब्जेक्ट की फ़ैमिली क्रिएट करने की आवश्यकता होती है।
जैसे, माना की कोई कंपनी और बैंक एम्प्लॉई एप्लिकेशन में कई अलग अलग तरह के वर्क कल्चर मेथड को सपोर्ट करता है.
Example of Abstract Factory pattern concept in Java.
// here we create a Abstract Product 1
interface Employee {
void work();
}
// here we create a Abstract Product 2
interface Manager {
void manage();
}
// here we create a Abstract Factory method
interface CompanyFactory {
Employee createEmployee();
Manager createManager();
}
// here we create a IT Employee
class ITEmployee implements Employee {
public void work() {
System.out.println(“IT Employee develops software”);
}
}
// here we create a IT Manager
class ITManager implements Manager {
public void manage() {
System.out.println(“IT Manager manages software projects”);
}
}
// here we create a Banking Employee
class BankEmployee implements Employee {
public void work() {
System.out.println(“Bank Employee handles customer accounts”);
}
}
// here we create a Banking Manager
class BankManager implements Manager {
public void manage() {
System.out.println(“Bank Manager manages banking daily operations”);
}
}
// here we create a IT Company Factory method
class ITCompanyFactory implements CompanyFactory {
public Employee createEmployee() {
return new ITEmployee();
}
public Manager createManager() {
return new ITManager();
}
}
// here we create a Banking Company Factory method
class BankCompanyFactory implements CompanyFactory {
public Employee createEmployee() {
return new BankEmployee();
}
public Manager createManager() {
return new BankManager();
}
}
// here we create a Main Class
public class Main {
public static void main(String[] args) {
// here we create a IT Company elements object
CompanyFactory factory = new ITCompanyFactory();
Employee emp = factory.createEmployee();
Manager mgr = factory.createManager();
emp.work();
mgr.manage();
System.out.println(“——–#########———-“);
// here we create a Banking Company element object
factory = new BankCompanyFactory();
emp = factory.createEmployee();
mgr = factory.createManager();
emp.work();
mgr.manage();
}
}
Here’s the Abstract Factory pattern main idea in this example.
एक क्लास फ़ैक्टरी पैटर्न → रिलेटेड कई क्लास ऑब्जेक्ट्स की फ़ैमिली को क्रिएट करता है
Main Difference Between Singleton, Factory, And Abstract Factory Method
| # | Pattern Method | Purpose of Each Pattern Method | Example |
| 1. | Singleton pattern | It helps user to create only one instance in single class | Logger events |
| 2. | Factory pattern | It helps to create one type of class object, hiding class creation logic expression when user need | Employee/Department factory |
| 3. | Abstract Factory pattern | It helps java user to create families of related class objects | Company/Works elements |
Conclusion of Singleton, Factory, and Abstract Factory.
- Singleton pattern – यह जावा क्लास में एक ऑब्जेक्ट को क्रिएट करता है.
- Factory pattern – यह जावा क्लास में एक फ़ैक्टरी → अलग-अलग प्रोडक्ट्स को क्रिएट करने में हेल्प करता है.
- Abstract factory pattern – यह जावा क्लास में फ़ैक्टरी → प्रोडक्ट्स की फ़ैमिली मल्टीप्ल इंडिविजुअल क्लास ऑब्जेक्ट मेथड को क्रिएट करता है.

