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
64
votes
10 answers

Singleton in go

How does one implement the Singleton design pattern in the go programming language?
Ben Noland
  • 34,230
  • 18
  • 50
  • 51
60
votes
9 answers

WPF: Cannot reuse window after it has been closed

I am trying to keep one instance of a Window around and when needed call ShowDialog. This worked find in winforms, but in WPF I recieve this exeception: System.InvalidOperationException: Cannot set Visibility or call Show, ShowDialog, or…
Jerod Houghtelling
  • 4,783
  • 1
  • 22
  • 30
60
votes
2 answers

How is Meyers' implementation of a Singleton actually a Singleton

I have been reading a lot about Singletons, when they should and shouldn't be used, and how to implement them safely. I am writing in C++11, and have come across the Meyer's lazy initialized implementation of a singleton, as seen in this…
lbrendanl
  • 2,626
  • 4
  • 33
  • 54
59
votes
7 answers

get_instance() in Codeigniter: Why assign it to a variable?

In Codeigniter, get_instance() is a globally available function that returns the Controller super-object which contains all the currently loaded classes (it returns the Controller class instance). I'll include the current source code: get_instance()…
Wesley Murch
  • 101,186
  • 37
  • 194
  • 228
59
votes
6 answers

Java best practice: Class with only static methods

I have an application where I have a class called PlausibilityChecker. This class has only static methods, like checkZipcodeFormat or checkMailFormat. I use them in my GUI classes to check the input before sending it to the lower layer. Is this good…
user3629892
  • 2,960
  • 9
  • 33
  • 64
58
votes
12 answers

Where exactly the Singleton Pattern is used in real application?

I was just curious where exactly the singleton pattern is used... I know how the pattern works and where it can be used but i personally never used in any real application. Can some one give an example where it can be used.. I would really…
swati
  • 719
  • 2
  • 7
  • 8
58
votes
9 answers

Is spring default scope singleton or not?

Could you please explain why Spring is creating two objects for the configuration of beans shown below, since by default spring default scope is singleton? The Spring configuration is here:
Raj
  • 677
  • 3
  • 8
  • 16
58
votes
10 answers

Purpose of singletons in programming

This is admittedly a rather loose question. My current understanding of singletons is that they are a class that you set up in such a way that only one instance is ever created. This sounds a lot like a static class to me. The main difference being…
thecoshman
  • 8,394
  • 8
  • 55
  • 77
57
votes
4 answers

Unit testing with singletons

I have prepared some automatic tests with the Visual Studio Team Edition testing framework. I want one of the tests to connect to the database following the normal way it is done in the program: string r_providerName =…
yeyeyerman
  • 7,751
  • 7
  • 43
  • 52
55
votes
4 answers

Multiple instances of singleton across shared libraries on Linux

My question, as the title mentioned, is obvious, and I describe the scenario in details. There is a class named singleton implemented by singleton pattern as following, in file singleton.h: /* * singleton.h * * Created on: 2011-12-24 * …
bourneli
  • 2,172
  • 4
  • 24
  • 40
54
votes
6 answers

Monostate vs. Singleton

What are the scenarios when one would use a Monostate pattern instead of singleton inorder to maintain a global object? Edit: I know what Singleton and Monostate patterns are. Have also implemented Singleton in quite a few scenarios. Just want to…
Rashmi Pandit
  • 23,230
  • 17
  • 71
  • 111
54
votes
14 answers

Singleton and unit testing

The Effective Java has the following statement on unit testing singletons Making a class a singleton can make it difficult to test its clients, as it’s impossible to substitute a mock implementation for a singleton unless it implements an interface…
user2434
  • 6,339
  • 18
  • 63
  • 87
54
votes
6 answers

Using RequireJS, how do I pass in global objects or singletons around?

Let's say I am writing code at the main page level and 2 dependencies require the same instance of an object and also state that as a dependency. What is the appropriate way to go about this? Basically what I want to do is say, "If this dependency…
egervari
  • 22,372
  • 32
  • 121
  • 175
54
votes
4 answers

When to use Spring prototype scope?

I want to know when should I exactly use the prototype scope in Spring? I have understood that singleton returns the same object instance if the bean is requested for. Then why should we consider prototype? Explanations with examples would help a…
Rahul
  • 619
  • 1
  • 5
  • 15
54
votes
5 answers

Is DocumentBuilder thread safe?

The current code base that I am looking at uses the DOM parser. The following code fragment is duplicated in 5 methods : DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder =…
Chetan Kinger
  • 15,069
  • 6
  • 45
  • 82