सिंगलटन डिज़ाइन पैटर्न
सिंगलटन एक डिज़ाइन पैटर्न है जो किसी क्लास के इंस्टेंशिएशन को केवल एक इंस्टेंस तक सीमित करता है। इसके उल्लेखनीय उपयोगों में कंकरेंसी नियंत्रण और एप्लिकेशन के लिए डेटा स्टोर तक केंद्रीय पहुंच बिंदु बनाना शामिल है।
उदाहरण: बेसिक सिंगलटन डायलॉग
यह उदाहरण दर्शाता है कि Android में सिंगलटन पैटर्न का उपयोग करके प्रोग्रेस डायलॉग कैसे प्रबंधित किया जाए।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में File → New Project चुनें और प्रोजेक्ट बनाएं।
चरण 2: लेआउट फ़ाइल (res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<Button
android:id="@+id/start"
android:text="सिंगलटन में डायलॉग शुरू करें"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/end"
android:text="सिंगलटन डायलॉग बंद करें"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
चरण 3: MainActivity.java
package com.example.singletondialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button start;
SingletonExample singletonExample;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start = findViewById(R.id.start);
singletonExample = SingletonExample.getInstance();
findViewById(R.id.end).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
singletonExample.dismiss();
}
});
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
singletonExample.show(MainActivity.this);
}
});
}
}
चरण 4: सिंगलटन क्लास (SingletonExample.java)
package com.example.singletondialog;
import android.app.Dialog;
import android.content.Context;
import android.view.Window;
public class SingletonExample {
private Dialog dialog;
private static final SingletonExample ourInstance = new SingletonExample();
public static SingletonExample getInstance() {
return ourInstance;
}
private SingletonExample() { }
public void show(Context context) {
if (dialog != null && dialog.isShowing()) {
return;
}
dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.layout_progress_dialog);
dialog.setCancelable(true);
dialog.show();
}
public void dismiss() {
if (dialog != null && dialog.isShowing()) {
dialog.dismiss();
}
}
}
चरण 5: डायलॉग लेआउट (res/layout/layout_progress_dialog.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/progress" />
</LinearLayout>
ऐप चलाएं
डिवाइस कनेक्ट करें और ऐप चलाएं। "सिंगलटन में डायलॉग शुरू करें" पर क्लिक करने से सिंगलटन क्लास से प्रोग्रेस डायलॉग दिखेगा। "सिंगलटन डायलॉग बंद करें" से इसे डिसमिस कर सकते हैं।
पिछले उदाहरण से अंतर
इस संस्करण में getInstance() मेथड synchronized नहीं है। यह सिंगल-थ्रेडेड उपयोग के लिए उपयुक्त है, लेकिन मल्टी-थ्रेडेड वातावरण में रेस कंडीशन का कारण बन सकता है।
मुख्य बिंदु
- सिंगलटन पैटर्न डायलॉग इंस्टेंस को केंद्रीकृत करता है
isShowing()जांच से डुप्लीकेट डायलॉग रोके जाते हैं- कस्टम लेआउट के साथ प्रोग्रेस बार दिखाया जाता है
- मल्टी-थ्रेडेड ऐप्स के लिए
synchronizedसंस्करण (पिछला लेख) बेहतर है