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

Protocol defining singleton with properties

I have the following protocol that defines a singleton with property: protocol SingletonProtocol { static var shared: SingletonProtocol { get } var variable : Int { get set } } And the following class that implements this protocol: class…
amir
  • 1,332
  • 8
  • 15
3
votes
3 answers

Double-check locking issues, c++

I left the rest of implementation for simplicity because it is not relevant here. Consider the classical implemetation of Double-check loking descibed in Modern C++ Design. Singleton& Singleton::Instance() { if(!pInstance_) { …
Eduard Rostomyan
  • 7,050
  • 2
  • 37
  • 76
3
votes
1 answer

Singleton translation unit confusion

In ContainerTest.h I have the following singleton class defined: class ContainerTest { private: ContainerTest() { test = InitializeTest(); } ContainerTest(const ContainerTest&) = delete; …
Bob Dole
  • 33
  • 2
3
votes
2 answers

Singelton lifetime within a dll / bundle

If I create a singleton class in the context of a dll or bundle on mac, the singleton class is instantiated once and used by all instances of the dll. I am using a dll as a plug-in for an application. Now the following thing came to my mind: If I…
st-h
  • 2,444
  • 6
  • 37
  • 60
3
votes
2 answers

Double checked locking without using volatile-keyword and without synchronizing the entire getInstance() method

Following is my singleton class where I am using double-checked-locking without using volatile keyword and without synchronizing the entire getInstance() method: public class MySingleton { private static MySingleton mySingleton; public…
3
votes
1 answer

Subcomponent (unscoped) may not reference scoped bindings: @Singleton @Provides @org.jetbrains.annotations.NotNull

I am using dagger 2.11 Module @Module class MyModule { @Provides fun provideString() : String = "yo" @Provides @Named("injector") fun provideInzectorString() : String = "named_injection" @Singleton @Provides //The error goes…
Ashwin
  • 12,691
  • 31
  • 118
  • 190
3
votes
2 answers

PHP: Is implementing the database layer as a singleton acceptable? Code inside

I know singletons are bad. But is it bad for this, too? class DaoMySQL { private static $instance; private $PDO; private function __construct() { $this->PDO = new…
user479911
3
votes
1 answer

why singleton design pattern allowing copy of object even copy constructor and assignment operator are private?

I have created below singleton class and defined copy constructor and assignment operator as a private. When I invoke copy constructor or assignment operator, it does not call copy constructor and assignment operator(Maybe due to statically object…
3
votes
5 answers

Multiple DB connection class

How do you create multiple DB connections using Singleton pattern? Or maybe there's better approach, to share the same class but multiple connections?
Deniss Kozlovs
  • 4,761
  • 2
  • 28
  • 35
3
votes
1 answer

Forcing singleton pattern in shared external modules

Let's say we have 2 js modules (A, B) that require a common module 'C' which is a Singleton or has static methods. If they both require the same instance of C then everything is fine. But if for some reason the require method will resolve to…
S.Kraus
  • 369
  • 4
  • 13
3
votes
2 answers

How to resolve singleton in Lumen/Laravel?

I have class which requires API Key (APIClent.php). I want to initialise APIClient.php and share instance (singleton) I have two controllers that need access to initialised instance (above). Now each time I call my controller, it's getting new a…
3
votes
2 answers

Use of volatile in Singleton(Bill Pughs Solution)

Given below is a java class using Bill Pugh singleton solution. public class Singleton { int nonVolatileVariable; private static class SingletonHelper { private static Singleton INSTANCE = new Singleton(); } private…
Deepak Shajan
  • 911
  • 1
  • 7
  • 19
3
votes
2 answers

How to make Singleton in Swift

I want to use Singleton to show ads, but it doesn't work well. When I don't use Singleton and use only ViewController, it works well.(can through "vampLoadStart" and "vampDidReceive") How can I solve it? Pattern1: when I use Singleton (can't load…
K.K.D
  • 917
  • 1
  • 12
  • 27
3
votes
1 answer

How to consider singleton in production code?

I have some java classes in which the members variables are all Autowired using Spring beans. I think that guarantees the singleton pattern. But I wonder how does the code run on a production server? Is singleton guaranteed per thread or is it…
Kozuki
  • 97
  • 8
3
votes
1 answer

Is this code correct example of thread safe Singleton design pattern?

Is below code correct example of thread safe Singleton pattern in Java ? class Singleton { private static Singleton INSTANCE = new Singleton(); public static Singleton getInstance() { return INSTANCE; } } Since static…
userv
  • 2,527
  • 4
  • 27
  • 36
1 2 3
99
100