I am working on my app and i confuse in between singleton class
object and object from my base class, both provide that single
instance use again and again. what is actually difference and
advantage of singleton?
and which approach is best.

- 23
- 6
3 Answers
Singleton is a way of organizing your code (aka design pattern) in such a way that a Singleton class can have one and only one instance in an application. this can be achieved in different ways: What is an efficient way to implement a singleton pattern in Java?.

- 1
- 1

- 341
- 1
- 13
-
@Alexander Dascalu yes i understand, the same thing i can achieve by making a object in base class ussing all over tha app, it also provide my same and one object.. plz elaburate some more and make me clear. – Umair Irshad Dec 15 '16 at 07:57
-
Like a said, the Singleton pattern is just a design pattern. Definitely, you can achieve the same behavior in different ways. But in what you are saying, you can create other instances of the same class. If another developer reads your code, it is not very clear that you intended to have only one instance. Using the Singleton pattern you make your intentions clear. – Alexandru Dascălu Dec 15 '16 at 08:02
Singleton design paradigm is applicable to any programming language that adheres to OOP. Mainly it's usage is that when having a singleton instance you save memory for creating multiple objects. But the singleton object should only serve one set of functionality to it's clients.
The best example is when working with GUIs users don't want multiple windows to edit single set of form details. In this scenario singleton can come in handy. So no matter how many times user launches a UI the same instance is returned every time.

- 2,529
- 1
- 21
- 33
-
what if i create a object in base class and use it again an again, is alse save memory.. please make me clear. – Umair Irshad Dec 15 '16 at 08:01
-
what you need to understand is whether it is singleton or not you need to decide which functionality you want if it is provided by super class then create it's singleton.. but if you do its derived class singleton you already have an instance of it's parent but this is not singleton( parent ). – Juliyanage Silva Dec 15 '16 at 09:34
singleton object has only one instance, so you can use the properties in global. the base object can create many instances, every instance has it's memory address, so the properties in every instance are different. we can't say which one is better, they are using in different condition.

- 1
- 2