Reputation: 73
I am developing an android application where i have a singleton class as below:
public class ClassName {
private static ClassName mobject;
public List<Question> mquestionslist;
public ClassName() {
mquestionslist=new ArrayList<Question>();
}
public static ClassName getInstance() {
if (mobject == null) {
mobject = new ClassName();
}
return mobject;
}
public void LoadQuestions()
{
//do somthing
}
}
Now Everytime i want to use this object, i just call ClassName.getInstance().any_method_name() from other activity or fragment and use it directly. I never create any local reference to this class object like:
ClassName ref=ClassName.getInstance();
ref.any_method_name();
=>Please tell me for how much time this object will survive before garbage collector removes it from memory and is it a good practice. (was not able to get answer from elsewhere)
Upvotes: 4
Views: 1530
Reputation: 4513
With the type of question you asked, It seems like your knowledge about the design patterns is limited. So let me add some description to it.
Read this document to know about different design patterns.
This particular link has definition and explanation of each design pattern, please read this once. Below I will mention the use of few.
Singleton:
Should be used if you need one and only one instance of a class. Now what makes you decide that you need only one instance?
Cases like downloading multiple files should be handled in a single thread, so download manager should be singleton, cases like handling database requires ACID implementation, so database handler should be singleton. That is, if you don't want to serialize your operations, you shouldn't use singleton.
Factory:
Factory methods are generally used in Fragments in Android. Reason?
Consider you have a viewpager, where the type of content in viewpager is different with something in common, so you can have a common newInstance
method in your super Fragment and here you can handle the logic to decide which fragment to pass to the viewpager
based on position and condition, this will simplify your code as you will isolate the display logic with function logic. Factory also allows you to maintain Fragment arguments.
Builder:
When you have a class which can be used directly without any parameters or by modifying individual parameters. In this case you can not have constructor for each combination of the parameter, though you can use setters and getters, but consider the case of first time initialization of individual variables. Here we use Builder. For example Glide or Picasso.
Conclusion:
You must be familiar with the purpose, once your purpose is crystal clear, you can pick up the design pattern accordingly. If you stick to one type of design pattern without knowing its pattern, then you will end up complicating your app. And the most important thing, if you follow the correct design pattern, your app will become efficient automatically and you need not worry about memory and processing in most of the cases.
Hope this will help you decide which pattern to pick. Let me know if you want to know more. And one thing I suggest is to put your exact requirement in question so that I or someone else can suggest best pattern for that purpose.
Upvotes: 1
Reputation: 3424
The instance you get by calling the 'getInstance' method is a static object defined in class 'ClassName'.
private static ClassName mobject;
The instance will not become eligible for garbage collection until the class 'ClassName' remains loaded.
If the classloader, used to load 'ClassName', becomes garbage collected then the class 'ClassName' will be unloaded and the respective static object will be lost.
Upvotes: 3