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
2166
votes
36 answers

What are drawbacks or disadvantages of singleton pattern?

The singleton pattern is a fully paid up member of the GoF's patterns book, but it lately seems rather orphaned by the developer world. I still use quite a lot of singletons, especially for factory classes, and while you have to be a bit careful…
Ewan Makepeace
  • 5,376
  • 9
  • 31
  • 31
2036
votes
42 answers

Difference between static class and singleton pattern?

What real (i.e. practical) difference exists between a static class and a singleton pattern? Both can be invoked without instantiation, both provide only one "Instance" and neither of them is thread-safe. Is there any other difference?
Jorge Córdoba
  • 51,063
  • 11
  • 80
  • 130
1621
votes
37 answers

Creating a singleton in Python

This question is not for the discussion of whether or not the singleton design pattern is desirable, is an anti-pattern, or for any religious wars, but to discuss how this pattern is best implemented in Python in such a way that is most pythonic. In…
theheadofabroom
  • 20,639
  • 5
  • 33
  • 65
1002
votes
6 answers

class << self idiom in Ruby

What does class << self do in Ruby?
randombits
  • 47,058
  • 76
  • 251
  • 433
890
votes
24 answers

C++ Singleton design pattern

Recently I've bumped into a realization/implementation of the Singleton design pattern for C++. It has looked like this (I have adopted it from the real-life example): // a lot of methods are omitted here class Singleton { public: static…
Artem Barger
  • 40,769
  • 9
  • 59
  • 81
850
votes
29 answers

What is an efficient way to implement a singleton pattern in Java?

What is an efficient way to implement a singleton design pattern in Java?
Riyaz Mohammed Ibrahim
  • 9,505
  • 5
  • 26
  • 33
608
votes
17 answers

How to declare global variables in Android?

I am creating an application which requires login. I created the main and the login activity. In the main activity onCreate method I added the following condition: public void onCreate(Bundle savedInstanceState) { …
Niko Gamulin
  • 66,025
  • 95
  • 221
  • 286
595
votes
30 answers

Using a dispatch_once singleton model in Swift

I'm trying to work out an appropriate singleton model for usage in Swift. So far, I've been able to get a non-thread safe model working as: class var sharedInstance: TPScopeManager { get { struct Static { static var instance:…
David Berry
  • 40,941
  • 12
  • 84
  • 95
564
votes
24 answers

On design patterns: When should I use the singleton?

The glorified global variable - becomes a gloried global class. Some say breaking object-oriented design. Give me scenarios, other than the good old logger where it makes sense to use the singleton.
Setori
  • 10,326
  • 11
  • 40
  • 46
525
votes
21 answers

Is there a simple, elegant way to define singletons?

There seem to be many ways to define singletons in Python. Is there a consensus opinion on Stack Overflow?
Jamie
  • 7,635
  • 4
  • 23
  • 16
483
votes
27 answers

How do you build a Singleton in Dart?

The singleton pattern ensures only one instance of a class is ever created. How do I build this in Dart?
Seth Ladd
  • 112,095
  • 66
  • 196
  • 279
456
votes
46 answers

Simplest/cleanest way to implement a singleton in JavaScript

What is the simplest/cleanest way to implement the singleton pattern in JavaScript?
Jakub Arnold
  • 85,596
  • 89
  • 230
  • 327
377
votes
10 answers

Singletons vs. Application Context in Android?

Recalling this post enumerating several problems of using singletons and having seen several examples of Android applications using singleton pattern, I wonder if it's a good idea to use Singletons instead of single instances shared through global…
Martín Schonaker
  • 7,273
  • 4
  • 32
  • 55
347
votes
10 answers

Create singleton using GCD's dispatch_once in Objective-C

If you can target iOS 4.0 or above Using GCD, is it the best way to create singleton in Objective-C (thread safe)? + (instancetype)sharedInstance { static dispatch_once_t once; static id sharedInstance; dispatch_once(&once, ^{ …
Ryan
  • 10,041
  • 27
  • 91
  • 156
333
votes
26 answers

What should my Objective-C singleton look like?

My singleton accessor method is usually some variant of: static MyClass *gInstance = NULL; + (MyClass *)instance { @synchronized(self) { if (gInstance == NULL) gInstance = [[self alloc] init]; } …
schwa
  • 11,962
  • 14
  • 43
  • 54
1
2 3
99 100