Questions tagged [singleton]

A design pattern that ensures that exactly one application-wide instance of a particular class exists. One of the Gang of Four's creational design patterns.

Singleton is a design pattern that restricts the instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system. The concept is sometimes generalized to systems that operate more efficiently when only one object exists, or that restrict the instantiation to a certain number of objects. The term comes from the mathematical concept of a singleton.

public class SingletonDemo {
    private static volatile SingletonDemo instance = null;

    private SingletonDemo() {
    }

    public static SingletonDemo getInstance() {
        if (instance == null) {
            synchronized (SingletonDemo.class){
                if (instance == null) {
                    instance = new SingletonDemo ();
                }
            }
        }
        return instance;
    }
}

Implementations of Singleton may include additional features such as thread-safe initialization, or some form of initialization order.

Singleton is arguably the most well-known, the most used, the most abused, and the most disputed design pattern in existence, frequently leading to heated discussions between its proponents and opponents.

This is one of the Gang of Four's creational , first published in Gamma et al.'s book "Design Patterns: Elements of Reusable Object-Oriented Software".

More information is available on Wikipedia.

8550 questions
34
votes
5 answers

Using different classloaders for different JUnit tests?

I have a Singleton/Factory object that I'd like to write a JUnit test for. The Factory method decides which implementing class to instantiate based upon a classname in a properties file on the classpath. If no properties file is found, or the…
matt b
  • 138,234
  • 66
  • 282
  • 345
34
votes
2 answers

Singleton via enum way is lazy initialized?

This is a very wide-spread enum singleton code: public enum enumClazz{ INSTANCE enumClazz(){ //do something } } and a bunch of places said it is a lazy initialization. But I am confused after I read Chapter 7 of 'Inside the Java…
vash_ace
  • 499
  • 2
  • 6
  • 11
34
votes
6 answers

Why choose a static class over a singleton implementation?

The Static Vs. Singleton question has been discussed before many times in SO. However, all the answers pointed out the many advantages of a singleton. My question is - what are the advantages of a static class over a singleton? Why not simply choose…
Elad
  • 19,079
  • 18
  • 62
  • 71
33
votes
6 answers

How to delete Singleton pointer?

I was implementing a singleton pattern.Here,I am creating a new instance of Singleton* in GetInstance, when I try and delete it in the destructor, it does in infinite loop. How to avoid memory leak in this case ? Please refer the below piece of…
Atul
  • 501
  • 1
  • 5
  • 17
33
votes
4 answers

Singleton instance declared as static variable of GetInstance method, is it thread-safe?

I've seen implementations of Singleton patterns where instance variable was declared as static variable in GetInstance method. Like this: SomeBaseClass &SomeClass::GetInstance() { static SomeClass instance; return instance; } I see following…
okutane
  • 13,754
  • 10
  • 59
  • 67
33
votes
1 answer

Are Kotlin's singletons thread safe?

Are Kotlin singletons (more specifically, object declarations) thread-safe by construction? If not, what is the best practice to write thread safe singletons in Kotlin? I would guess they are, but I haven't been able to find any explicit statement…
alfongj
  • 2,255
  • 1
  • 19
  • 23
33
votes
6 answers

Java: getInstance vs Static

What is the purpose of getInstance() in Java? During my research I keep reading that getInstance() helps achieve a Singleton design pattern (which means just one instance across the whole program to my understanding). But can't I just use static?…
FreakyDan
  • 559
  • 1
  • 7
  • 24
33
votes
10 answers

Is there a name meaning "not a singleton"?

Is there a name meaning "not a singleton"?
Richard Nagle
  • 11,974
  • 3
  • 21
  • 16
33
votes
5 answers

Thread safe instantiation of a singleton

Which one synchronization method to use to ensure a singleton remains a singleton? +(Foo*)sharedInstance { @synchronized(self) { if (nil == _sharedInstance) { _sharedInstance = [[Foo alloc] init]; ... } …
33
votes
8 answers

How to abstract a singleton class?

This is how I write my singleton classes. public class MyClass { /// /// Singleton /// private static MyClass instance; /// /// Singleton access. /// public static MyClass…
Reactgular
  • 52,335
  • 19
  • 158
  • 208
32
votes
8 answers

Yeah.. I know.. I'm a simpleton.. So what's a Singleton?

I've tried a few times to understand what a Singleton is. Perhaps I'm just too visual.. so can anyone break it down in a simple analogy. Similar Posts: Different ways to initialize singletons Singleton: How should it be used Is this a good use of…
madcolor
  • 8,105
  • 11
  • 51
  • 74
32
votes
1 answer

What is a scoped proxy in Spring?

As we know Spring uses proxies to add functionality (@Transactional and @Scheduled for example). There are two options - using a JDK dynamic proxy (the class has to implement non-empty interfaces), or generating a child class using the CGLIB code…
gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
32
votes
4 answers

My singleton can be called multiple times

I have implemented an singleton based on c++ 11. However the constructor can be called multiple times in some cases. The class will be compiled to static lib and used by other so lib (more than one so lib). And the system is a multi-thread system…
hismart
  • 405
  • 6
  • 15
32
votes
7 answers

How to create a singleton class

What is the best/correct way to create a singleton class in java? One of the implementation I found is using a private constructor and a getInstance() method. package singleton; public class Singleton { private static Singleton me; …
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
32
votes
6 answers

Singleton for Application Configuration

In all my projects till now, I use to use singleton pattern to access Application configuration throughout the application. Lately I see lot of articles taking about not to use singleton pattern , because this pattern does not promote of…
madhu
  • 497
  • 1
  • 6
  • 9