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
1 answer

Why does the explicit syntax for creating Tuples only allow AnyRefs as type annotations?

This code works: scala> val x = "" x: java.lang.String = "" scala> Tuple2[x.type, x.type](x,x) res5: (x.type, x.type) = ("","") This one doesn't: scala> val y = 0 y: Int = 0 scala> Tuple2[y.type, y.type](y,y) :9: error: type mismatch; …
soc
  • 27,983
  • 20
  • 111
  • 215
3
votes
8 answers

When should I use a singleton class in C++?

From a beginning C++ user's point of view the only reason I would see a singleton class useful is similar to when I want to use a static variable. Do you guys know when its most appropriate to use a singleton class?
Mark
  • 8,408
  • 15
  • 57
  • 81
3
votes
2 answers

Singleton managedObjectContext

I want to use the singleton UIApplication to access the managedObjectContext of the AppDelegate. But when I write [[[UIApplication sharedApplication] delegate] managedObjectContext] or [[[UIApplication sharedApplication] delegate]…
3
votes
2 answers

PHP5.3 : How to use PDO singleton class (indirectly) from the main source code

Using PHP5.3.3, I have 2 classes, called SinglePDO and Manager, both working, but I'd certainly need your advices to optimize those unefficient code scriptings. Hence I have 2 questions but I guess strictly related one to another : 1) Access to…
hornetbzz
  • 9,188
  • 5
  • 36
  • 53
3
votes
3 answers

Is this Singleton Implementation Thread-Safe?

I've read the question and answers here, and learned that a true thread-safe singleton can be implemented with correctly placed memory barrier, which is stated here in chapter 6. I've come up with my own version of singleton implementation, by…
SluggishNewb
  • 65
  • 2
  • 5
3
votes
1 answer

Undoing a decade of singleton pattern and class-level configuration

Overview I need to duplicate a whole inheritance tree of classes. Simply deep-copying the class objects does not work; a proper factory pattern involves a huge amount of code changes; I'm not sure how to use metaclasses to accomplish…
morphheus
  • 129
  • 5
3
votes
1 answer

How to use singleton pattern in React Native

I'm converting an Android application to React Native and I'm new to this react-native technology. In my android application, I have used the Singleton object to store some data. I just want to create the same for the react-native application.…
Pasindu Weerakoon
  • 588
  • 1
  • 11
  • 39
3
votes
1 answer

The initialization function is called twice in a `lazy_static` block

I have a big project Where I use lazy_static to create a singleton. I think there is a bug in lazy_static crate (which appears in big projects only) or I am doing something wrong because the initialization function which must be called once to…
asmmo
  • 6,922
  • 1
  • 11
  • 25
3
votes
1 answer

Java Generic Singleton Factory Pattern

I am having a stump of a time understanding the condundrum below. Here is a code snippet that compiles, yet throws the exception Exception in thread "main" java.lang.ClassCastException: TestGenericSingleton$$Lambda$1/303563356 cannot be…
3
votes
1 answer

Destruction of local static objects

Consider a situation like this: #include int foo() { static struct S { int value; S(int a): value(a) {}~S() { std::cout << "End is nigh"; } } s(42); return s.value; } int main() { …
Swift - Friday Pie
  • 12,777
  • 2
  • 19
  • 42
3
votes
3 answers

Should I use static at all in spring singleton beans

As I just read, by default my created spring beans are Singletons. I'm currently using the static keyword for my Loggers, reused variables, and some Lists that I want to exist only once. But because alls beans are singletons I'm thinking about just…
3
votes
2 answers

Why enum singleton is lazy?

I saw answers like these, tried to clarify via comments, and was unsatisfied by examples here. Maybe it's time for this specific question... Why enum singleton implementation is called lazy? public enum EnumLazySingleton { INSTANCE; …
uvsmtid
  • 4,187
  • 4
  • 38
  • 64
3
votes
4 answers

What would be the correct design for keeping configuration settings in c++?

Say I have ini/json to store configuration setting of my desktop application,Will it be ok to have a static object with all property loaded at startup/when ever it is required or is there any other better alternative? Since this is the very first…
yesraaj
  • 46,370
  • 69
  • 194
  • 251
3
votes
5 answers

What do you call a "singleton" that allows a public constructor for additional instances?

I'm making a class that supports the Singleton use (one instance is available), yet it also supports normal instance use (via a public constructor). If you only want one, use that one. If you want 5, new them up. I clearly can't call this…
Amy B
  • 108,202
  • 21
  • 135
  • 185
3
votes
4 answers

Disposing a singleton instance (C#)

If a singleton implements IDisposable, what is the right way of disposing and re-creating an instance? One way would be to keep _disposed flag and check for it in the Instance property, but I'm not sure it is the right way to do this. A simple…
kateroh
  • 4,382
  • 6
  • 43
  • 62