Purpose of Singleton:
The main purpose of the singleton class is to object
creation, limiting the number of objects to only one. Since there is only one
singleton instance fields of a singleton will occur only once per class, just
like static.
Prons:
1. Singleton prevents other objects from instantiating their
own copies of the singleton object, ensuring that all objects access the single
instance.
2. Since the class controls the instantiation process, the
class has the flexibility to change the instantiation process.
3. The advantage of singleton over global variable is that you are
absolutely sure of the number of instances when you use Singleton.
Cons:
1. Their state is automatically shared across the entire app.
2. Bugs can often start occurring when that state changes
unexpectedly.
3. The relationships between singleton and the code that depends
on them is usually not very well defined.
4. Managing their life-cycle can be tricky.
How to implement Singleton class.
Step 1. Create a class.
public class MyApplication {
}
Step 2. Create an instance of the class which declare as a static.
public class MyApplication { private static MyApplication appInstance;}
Step 3. Make the initializer. final Singleton class
public class MyApplication { private static MyApplication appInstance; public static synchronized MyApplication getAppInstance(){ return appInstance; }
}
public class MyApplication extends Application { private static MyApplication appInstance; @Override public void onCreate() { super.onCreate(); appInstance=this; } public static synchronized MyApplication getAppInstance(){ return appInstance; } }
How to used Singleton class in Activity in android.
In this example we have save the login details in sharedpreferences storage.
import android.app.Application; import android.content.SharedPreferences; import com.android.volley.Request; import com.android.volley.RequestQueue; import com.android.volley.toolbox.Volley; public class MyApplication extends Application {
private final static String PREFS_NAME = "SaveValue"; private static MyApplication appInstance; @Override public void onCreate() { super.onCreate(); appInstance=this; } public static synchronized MyApplication getAppInstance(){ return appInstance; }
public void saveString(String key, String data) { SharedPreferences settings = MyApplication.appInstance.getSharedPreferences(PREFS_NAME, 0); SharedPreferences.Editor editor = settings.edit(); editor.putString(key, data); editor.commit(); } public String getString(String key) { SharedPreferences settings = MyApplication.appInstance.getSharedPreferences(PREFS_NAME, 0); return settings.getString(key,""); } }
Activity
public class MainActivity extends AppCompatActivity {
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
MyApplication..getAppInstance()
// Save login Details. MyApplication.getAppInstance().
saveString("Username", loginRequest.getUserName());
MyApplication.getAppInstance().
saveString("Password", loginRequest.getPassword());
// Get login Details
MyApplication.getAppInstance().getString("Username");MyApplication..getAppInstance().getString("Password");}
}
0 comments