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
54
votes
12 answers

Are Singletons really that bad?

Possible Duplicate: What is so bad about Singletons? It's understandable that many design patterns can in some cases be abused, and like mom always said: "Too much of a good thing isn't always good!" I'm noticing that these days, I'm using…
josef.van.niekerk
  • 11,941
  • 20
  • 97
  • 157
53
votes
4 answers

Kotlin singleton application class

On Android I want to make my application class a singleton. Making it like this: object MyApplication: Application(){} won't work. The following error is thrown at runtime: java.lang.IllegalAccessException: private com....is not accessible from…
johnny_crq
  • 4,291
  • 5
  • 39
  • 64
52
votes
7 answers

When should the Singleton pattern NOT be used? (Besides the obvious)

I know well that you want to use Singleton to provide a global point of access to some state or service. The benefits of the Singleton pattern do not need to be enumerated in this question. What I am interested in are the situations when Singleton…
Mike
  • 19,267
  • 11
  • 56
  • 72
51
votes
13 answers

WPF Single Instance Best Practices

This is the code I implemented so far to create a single instance WPF application: #region Using Directives using System; using System.Globalization; using System.Reflection; using System.Threading; using System.Windows; using…
Tommaso Belluzzo
  • 23,232
  • 8
  • 74
  • 98
51
votes
7 answers

Problems with Singleton Pattern

I've been reading about Singleton pattern for last few days. The general perception is that the scenarios where it is required are quite few (if not rare) probably because it has its own set of problems such as In a garbage collection environment…
Ankur
  • 11,239
  • 22
  • 63
  • 66
50
votes
9 answers

ok , global variable is condemned, singleton is despised, what's the alternative?

For desktop application that is. This is just a general question that maybe only need general answers.
mhd
  • 4,561
  • 10
  • 37
  • 53
49
votes
7 answers

Why use singleton instead of static class?

When would a singleton actually be easier or better than a static class? It seems to me creating a singleton is just extra effort that's not actually needed, but I'm sure there is a good reason. Otherwise, they wouldn't be used, obviously.
Jouke van der Maas
  • 4,117
  • 2
  • 28
  • 35
49
votes
7 answers

Serialize a Static Class?

What happens if we serialize a static class? Can more than one instance of the static class be created if we serialize it? [Serializable] public static class MyClass { public static MyClass() { } public static bool IsTrue() { …
Bhaskar
  • 10,537
  • 6
  • 53
  • 64
46
votes
12 answers

Singleton: How to stop create instance via Reflection

I know in Java we can create an instance of a Class by new, clone(), Reflection and by serializing and de-serializing. I have create a simple class implementing a Singleton. And I need stop all the way one can create instance of my Class. public…
Talha Ahmed Khan
  • 15,043
  • 10
  • 42
  • 49
46
votes
14 answers

How to create singleton class in objective C

How can I create a singleton class in Objective C?
iOSAppDev
  • 2,755
  • 4
  • 39
  • 77
46
votes
8 answers

Singleton pattern in C++

I have a question about the singleton pattern. I saw two cases concerning the static member in the singleton class. First it is an object, like this class CMySingleton { public: static CMySingleton& Instance() { static CMySingleton…
skydoor
  • 25,218
  • 52
  • 147
  • 201
46
votes
5 answers

What is the point of making the singleton instance volatile while using double lock?

private volatile static Singleton uniqueInstance In a singleton when using double lock method for synchronization why is the single instance declared as volatile ? Can I achieve the same functionality without declaring it as volatile ?
45
votes
1 answer

Thread Safe Singletons in Java

The wikipedia article on Singletons mentions a few thread safe ways to implement the structure in Java. For my questions, let's consider Singletons that have lengthy initialization procedures and are acccessed by many threads at once. Firstly, is…
donnyton
  • 5,874
  • 9
  • 42
  • 60
45
votes
6 answers

Singleton with parameters

I need a singleton class to be instantiated with some arguments. The way I'm doing it now is: class SingletonExample { private SingletonExample mInstance; //other members... private SingletonExample() { } public…
veljkoz
  • 8,384
  • 8
  • 55
  • 91
45
votes
5 answers

C++ singleton vs completely static object

Let say we need to have just one instance of some class in our project. There are couple ways of doing it. I want to compare. Please can you review my understanding. 1) Classical Singleton pattern 2) Completely static class (all methods and members…
Victor Ronin
  • 1,101
  • 2
  • 9
  • 5