सिंगलटन पैटर्न और टेक्स्ट-टू-स्पीच
सिंगलटन डिज़ाइन पैटर्न किसी क्लास को एक इंस्टेंस तक सीमित करता है। इस उदाहरण में हम सिंगलटन क्लास के माध्यम से Android के TextToSpeech इंजन का उपयोग करेंगे।
उदाहरण: सिंगलटन TTS
चरण 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">
<EditText
android:id="@+id/editText"
android:hint="यहां बोलने के लिए टेक्स्ट लिखें"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/show"
android:text="सिंगलटन से TTS"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
चरण 3: MainActivity.java
package com.example.singletontts;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
Button show;
SingletonExample singletonExample;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
show = findViewById(R.id.show);
singletonExample = SingletonExample.getInstance();
singletonExample.init(getApplicationContext());
final EditText editText = findViewById(R.id.editText);
show.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String text = editText.getText().toString();
if (!text.isEmpty()) {
singletonExample.textToSpeech(text);
}
}
});
}
}
चरण 4: सिंगलटन क्लास (SingletonExample.java)
package com.example.singletontts;
import android.content.Context;
import android.speech.tts.TextToSpeech;
import java.util.Locale;
public class SingletonExample {
private static TextToSpeech tts;
private static SingletonExample ourInstance = new SingletonExample();
private Context appContext;
private SingletonExample() { }
public static Context get() {
return getInstance().getContext();
}
public static synchronized SingletonExample getInstance() {
return ourInstance;
}
public void init(Context context) {
if (appContext == null) {
this.appContext = context;
}
}
private Context getContext() {
return appContext;
}
public void textToSpeech(String text) {
if (tts == null) {
tts = new TextToSpeech(get(), new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status != TextToSpeech.ERROR) {
tts.setLanguage(Locale.UK);
}
}
});
}
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null, null);
}
}
ऐप चलाएं
ऐप लॉन्च करें, एडिटटेक्स्ट में कुछ टाइप करें और "सिंगलटन से TTS" बटन दबाएं। सिंगलटन क्लास से टेक्स्ट-टू-स्पीच चलेगा।
महत्वपूर्ण सुधार (मूल कोड में बग फिक्स)
- मूल कोड हर क्लिक पर नया
TextToSpeechऑब्जेक्ट बनाता था - यह गलत है - सुधार:
ttsको स्टेटिक रखें और केवल एक बार इनिशियलाइज़ करें QUEUE_FLUSHके साथ चौथा पैरामीटर (utteranceId) जोड़ा गया है- अनावश्यक इंपोर्ट हटाए गए (
MediaPlayer,View,Timerआदि)
मुख्य बिंदु
- सिंगलटन TTS इंजन को पुन: उपयोग करता है, संसाधन बचाता है
- ग्लोबल कॉन्टेक्स्ट से TTS इनिशियलाइज़ होता है
- भाषा सेटिंग (UK English) इनिशियलाइज़ेशन के बाद सेट होती है
- ऐप क्लोज होने पर
tts.shutdown()कॉल करना चाहिए (यहां नहीं दिखाया गया)