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.
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?.
2 Comments
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.