2

I went some interviews, there are asking the below question. Please help me.

Is singleton preferable for Android? Why?

I googled but I didn't get the correct solution. Please give me correct answer.

Ivaylo Slavov
  • 8,839
  • 12
  • 65
  • 108
Navadeep
  • 49
  • 1
  • 10
  • "Singleton" is a pattern. You need to be more specific about what "preferable" means to get any answer... – shkschneider May 07 '14 at 09:50
  • Singleton is a design pattern and can be used anywhere wherever you require a single instance of a class... it can be android, c++, java, Objective C anything... it depends on your requirement – AurA May 07 '14 at 09:50
  • it depends on your needs! and singleton is a design pattern – Arash GM May 07 '14 at 09:50
  • I think the answer to this is opinion really, as it always has its pros and cons. So it depends on the requirement and preference – IAmGroot May 07 '14 at 09:51
  • 1
    If it was an interview question then there is probably no right or wrong answer, they will have wanted you to talk about singleton and android, weighing up the pros and cons, in order to prove to them that you know about android and singleton. – man-qa May 07 '14 at 09:52
  • Was that for a job interview? If you do not know the answer, you can always ask the interviewer what it is - that is the best way to get the correct answer in the _sense being asked_. As others here point out, __Singleton__ is a design pattern that has wide usages. It depends on the scenario if it is appropriate or not and out of context this question has almost not any sense. – Ivaylo Slavov May 07 '14 at 09:53
  • Yes, It is a job interview. If you don't mind can you please give me one example in which cases this pattern is used? – Navadeep May 07 '14 at 09:56
  • Assuming you are not familiar with Singleton, this is a pattern that causes you to have only one single instance of a given class. This could be useful if the object can be kept as global for the entire application, and also it consumes a lot of resources to get instantiated. So, the singleton saves you time and memory as you will construct the object once only and use the same instance in all other cases. As you can see, the reasoning here is the object's constructor being heavy on the system. There might be other reasons behind this pattern usage, in Android, memory and CPU are limited . – Ivaylo Slavov May 07 '14 at 09:59

3 Answers3

2

Please check this Singletons vs. Application Context in Android? question. It contains extended description of pros and cons of singleton in your android app.

Community
  • 1
  • 1
amukhachov
  • 5,822
  • 1
  • 41
  • 60
1

I don't see anything specific to Android in the singleton pattern. It's valid or not based on the program needs/usage, not the platform.

schmop
  • 1,440
  • 1
  • 12
  • 21
0

I think this is a bad question (not the one I'm answering, but the one from the interview) because there isn't a correct and simple answer. It seems someone thought "Android developers have to be more aware about memory limitations than regular Java ones, so they should create as few instances as possible, so a Singleton pattern may help a lot".

But it's just a design pattern to be used when it fits, not as a rule of thumb. Pros and cons must be viewed for each case.

UPDATE: @Birdy's link explains it better.

Pablo Lozano
  • 10,122
  • 2
  • 38
  • 59