+ 6
Singleton
can anyone explain to me what is a singleton?
9 Réponses
+ 15
Simplest possible example... The code cannot not be run, but you can see how a singleton can be created:
https://code.sololearn.com/cfhabtq79XoJ/?ref=app
+ 16
Singleton is a design pattern, that allows only one instance of a class. You can use it if you need an object to be unique (only create a new object if the class wasn't instantiated before).
+ 11
You're welcome ^^
+ 3
Singleton is a programming pattern that provide a unique global instance. The class is responsible to create that unique instance.
+ 2
oh ok that makes sense thanks a lot
+ 2
It's a common used design pattern that restricts the instantiation of a class to one object.
Singleton wikipedia:
https://en.wikipedia.org/wiki/Singleton_pattern
+ 2
some days ago I demonstrated in another thread an example implementation of a singleton
https://code.sololearn.com/cQsAcUted9IO/?ref=app
+ 1
do you think you can give me an example or a link to an easy to understand singleton
+ 1
For any Java class if we are allowed to create only one object, then such type of class is called singleton class.
Some of the examples are:
Runtime
BusinessDeligate
ServiceLocator
The main advantage and central idea of singleton classes are, If several people have same requirement, then it's not recommended to create seperate object for every requirement.
We have to create only one object and we can reuse the same object as many times we want, so that performance and memory utilisation will be improved.
We can create our own singleton classes by using
1. private constructor
2. private static variable
3. public factory method.