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
3
votes
4 answers

What to use instead of static variables

in a C++ program I need some helper constant objects that would be instantiated once, preferably when the program starts. Those objects would mostly be used within the same translation unit, so the simplest way to do this would be to make them…
Roman L
  • 3,006
  • 25
  • 37
3
votes
2 answers

Kotlin property with getter. Can I not specify an initial value?

I want to create a singleton class, but unfortunately, Android needs Context for almost anything so I need it to create an instance. So I just assumed the user called init(), and then return the instance. As you see below, if the _instance is null,…
Damn Vegetables
  • 11,484
  • 13
  • 80
  • 135
3
votes
2 answers

A more compact way to do a singleton with a parameter

I am not really that good at patterns. (But I do realize that they are very important.) I have a scenario that needs a singleton but also needs to take a parameter. Normally for a singleton I just make a static method that does a: return…
Vaccano
  • 78,325
  • 149
  • 468
  • 850
3
votes
3 answers

Impact of multiple threads using same object in a singleton class

I'm designing a module which can support different datasources. My module gets the user's company id as inputs and I must call the appropriate class based on the company id. I'm trying to incorporate some good design and avoid conditional statements…
dozer
  • 861
  • 1
  • 11
  • 22
3
votes
3 answers

How to register singleton on Autofac

I am using Autofac for dependency injection in my .Net Core 2.0 web application. I am new to Autofacand dependency injection concepts. Now, I want to register a singleton in DI and resolve it in DI container. C# Code [Interface] public interface…
prog1011
  • 3,425
  • 3
  • 30
  • 57
3
votes
1 answer

A Singleton with multiple instances?

Background I have a situation in which I'm developing a program that runs like a thread. Basically, there is a "master" program that runs instances of sub-programs simultaneously. Due to the nature of this system, the "sub-programs" all run under a…
Clay07g
  • 1,105
  • 7
  • 23
3
votes
0 answers

python - Class based decorator as singleton

I wrote a function based decorator to handle the authentication to my company web services: def company_auth(path=None, method=None): """ Company Webservices authentication decorator :param path: relative url of the endpoint; :param…
Luke
  • 1,794
  • 10
  • 43
  • 70
3
votes
1 answer

Spark Object (singleton) serialization on executors

I am not sure that what I want to achieve is possible. What I do know is I am accessing a singleton object from an executor to ensure it's constructor has been called only once on each executor. This pattern is already proven and works as expected…
Alex Naspo
  • 2,052
  • 1
  • 20
  • 37
3
votes
1 answer

Singleton class: static properties or non-static properties?

I´m programming a Class which acts as a Singleton. I was wondering, does it make sense to have non-static properties for this class? Pseudocode example: class Foo extends MySingletonClass { private static string bar; private string baz; …
Yeroon
  • 3,223
  • 2
  • 22
  • 29
3
votes
3 answers

How to save get_instance call on Singleton in C++?

In this dummy example below, I would like to create a Singleton where I can save the get_instance call which is costly on embedded microcontrollers. #include template class CamelBase { public: static T& get_instance() { …
nowox
  • 25,978
  • 39
  • 143
  • 293
3
votes
2 answers

What is the difference between a Singleton and static factory methods

I want to know if both singleton and static factory method create only one instance then why there are two concepts for same purpose? Note: Here term "static factory method" is taken from Effective java book by Joshua bloch where he has written: "A…
3
votes
2 answers

Which is the best/most used singleton coding convention in Ruby?

In the Ruby Standard Library we have the Singleton class: http://ruby-doc.org/stdlib/libdoc/singleton/rdoc/index.html We can make any class a singleton by including this class inside. I just rarely see this used. When would it make sense to use this…
Thomas Watson
  • 6,507
  • 5
  • 33
  • 43
3
votes
2 answers

PHP constructors without parameters or executable code

While learning about design patterns I have come across the singleton pattern: class Singleton { private static $instance = null; private function __construct() { } public static function getInstance() { if…
jstyles85
  • 61
  • 4
3
votes
3 answers

c++, multiple instances of a dll, singleton

I have got a DLL in which a singleton is defined. I have got an app which can load multiple instances of this DLL. The DLL needs a singleton instance per DLL instance, otherwise it will crash. I observed that there was only one singleton instance…
moala
  • 5,094
  • 9
  • 45
  • 66
3
votes
1 answer

Why does a lot of popular wordpress plugins goes full Singletons

I was wondering recently as to why everybody says you should avoid Singletons, so I read about it and it makes sense. But I have noticed that a lot of popular Wordpress plugins are almost going Full Singletons in their design pattern. For a…
jayrchamp
  • 85
  • 1
  • 9
1 2 3
99
100