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
40
votes
5 answers

How to use scala.None from Java code

Possible Duplicate: Accessing scala.None from Java In Java you can create an instance of Some using the constructor, i.e. new Some(value), but None has no partner class. How do you pass None to a Scala function from Java?
Craig B.
  • 553
  • 1
  • 4
  • 10
39
votes
6 answers

Python: thinking of a module and its variables as a singleton — Clean approach?

I'd like to implement some sort of singleton pattern in my Python program. I was thinking of doing it without using classes; that is, I'd like to put all the singleton-related functions and variables within a module and consider it an actual…
user786233
39
votes
6 answers

C++, static vs. namespace vs. singleton

I already read a lot of posts and articles all over the net, but I couldn't find a definite answer about this. I have some functions with similar purposes that I want to have out of the global scope. Some of them need to be public, others should be…
blubberbernd
  • 3,641
  • 8
  • 35
  • 46
38
votes
9 answers

Thread safe lazy construction of a singleton in C++

Is there a way to implement a singleton object in C++ that is: Lazily constructed in a thread safe manner (two threads might simultaneously be the first user of the singleton - it should still only be constructed once). Doesn't rely on static…
pauldoo
  • 18,087
  • 20
  • 94
  • 116
38
votes
5 answers

Dagger injection not working for "object" in Kotlin

After spending a ludicrous amount of time trying to figure out why my dagger injections weren't working; I realised that the "object" type in Kotlin was the problem. The following did not work, the injected "property" was null. object SomeSingleton…
Nihilanth
  • 401
  • 1
  • 4
  • 8
37
votes
5 answers

Return same instance for multiple interfaces

I'm registering components with the following code: StandardKernel kernel = new StandardKernel(); string currentDirectory = Path.GetDirectoryName(GetType().Assembly.Location) foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) { …
jgauffin
  • 99,844
  • 45
  • 235
  • 372
37
votes
4 answers

StructureMap singleton usage (A class implementing two interface)

public interface IInterface1 { } public interface IInterface2 { } public class MyClass : IInterface1, IInterface2 { } ... ObjectFactory.Initialize(x => { x.For().Singleton().Use(); …
James
37
votes
9 answers

Unity singleton manager classes

In Unity, whats a good way to create a singleton game manager that can be accessed everywhere as a global class with static variables that will spit the same constant values to every class that pulls those values? And what would be the way to…
DeviArt
  • 469
  • 1
  • 5
  • 11
37
votes
6 answers

Why implementing a Singleton pattern in Java code is (sometimes) considered an anti-pattern in Java world?

I have seen some people in SO commenting that Singleton Pattern is an anti-pattern. I want to know why ?
Inquisitive
  • 7,476
  • 14
  • 50
  • 61
36
votes
4 answers

Explanation of singleton objects in Scala

I get the coding in that you basically provide an "object SomeClass" and a "class SomeClass" and the companion class is the class declaration and the object is a singleton. Of which you cannot create an instance. So... my question is mostly the…
gks
  • 785
  • 2
  • 7
  • 11
36
votes
14 answers

How to implement a singleton model

I have a site in rails and want to have site-wide settings. One part of my app can notify the admin by SMS if a specific event happens. This is an example of a feature that I want configurable via the site-wide settings. So I was thinking I should…
Fredrik
  • 4,161
  • 9
  • 28
  • 31
36
votes
3 answers

Difference between javax.inject.Singleton and javax.ejb.Singleton

im little confused. What is the exact difference between javax.inject.Singleton and javax.ejb.Singleton?
Hakan Kiyar
  • 1,199
  • 6
  • 16
  • 26
36
votes
4 answers

How thread-safe is enum in java?

How thread-safe is enum in java? I am implementing a Singleton using enum (as per Bloch's Effective Java), should I worry at all about thread safety for my singleton enum? Is there a way to prove or disprove that it is thread safe? // Enum singleton…
Lydon Ch
  • 8,637
  • 20
  • 79
  • 132
35
votes
3 answers

Kotlin thread safe native lazy singleton with parameter

In java we can write thead-safe singletons using double Checked Locking & volatile: public class Singleton { private static volatile Singleton instance; public static Singleton getInstance(String arg) { Singleton…
punksta
  • 2,738
  • 3
  • 23
  • 41
34
votes
6 answers

Singleton pattern (Bill Pugh's solution)

I'm reading wiki about the singleton pattern and I'm not sure if I understand this: https://en.wikipedia.org/wiki/Initialization-on-demand_holder_idiom part of it correctly. So to make it simple: Why is Bill Pugh's solution better than the example…
Rob Fox
  • 5,355
  • 7
  • 37
  • 63