How long will a singleton object last

I am developing an Android application where I have a singleton class as shown 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, every time I want to use this object, I just call ClassName.getInstance (). any_method_name () from another activity or fragment and use it directly. I never create a local reference for this class object, for example:

ClassName ref=ClassName.getInstance();
ref.any_method_name();

      

=> Please tell me how long this object will survive before the garbage collector removes it from memory, which is good practice. (couldn't get an answer from other sources)

+3


source to share


2 answers


The instance you get when you call the getInstance method is a static object defined in the "ClassName" class.

 private static ClassName mobject;

      



The instance will not be eligible for garbage collection until the ClassName is loaded.

If the classloader used to load "ClassName" becomes garbage collected, the ClassName will be unloaded and the associated static object will be lost.

+2


source


With the type of question you asked, it seems like your knowledge of design patterns is limited. So let me add some description to it.

Read this document to learn about the various design patterns.

This particular link has a definition and explanation of each design pattern, please read this once. Below I mentioned the use of a few.

Singleton:
Should be used if you need one and only one instance of a class. Now, what makes you decide that you only need one copy?
Multiple file upload cases must be handled in a single thread, so the download manager must be single-point, cases like database processing require an ACID implementation, so the database handler must be single-dot. That is, if you don't want to serialize your operations, you shouldn't use a singleton.



Factory:
Factory methods are commonly used in Fragments in Android. Cause?
You have a viewpager where the content type in the viewpager is different from something in common, so you can have a generic method newInstance

in your super fragment, and here you can process the logic to decide which fragment to go to viewpager

based on position and state , this will simplify your code as you isolate display logic with function logic. Factory also allows you to support fragment arguments.

Builder:
When you have a class that you can use directly without any parameters or by changing individual parameters. In this case, you cannot have a constructor for every parameter combination, although you can use setters and getters, but consider the case of initially initializing individual variables. We are using the Builder here. For example, Glide or Picasso.

Takeaway:
You should be familiar with this goal, once your goal is crystal clear, you can match your design template accordingly. If you stick to one type of design patterns without knowing its pattern, you end up complicating your application. Most importantly, if you follow the right design pattern, your application will be efficient automatically, and you don't have to worry about memory and processing in most cases.

Hope this helps you decide which template to choose. Let me know if you would like to know more. And I want to offer you the exact requirements due to the fact that I or someone else can offer the best sample for this purpose.

+2


source







All Articles