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

Angular 7 service not acting as a singleton

I used ng generate to create a service. I initialize an array and change a flag, but when I route to a different page the array is empty and the flag is false. My service starts with @Injectable({ providedIn: 'root' }) I understood that would…
3
votes
3 answers

Dependency Injection and/vs Global Singleton

I am new to dependency injection pattern. I love the idea, but struggle to apply it to my case. I have a singleton object, let’s call it X, which I need often in many parts of my program, in many different classes, sometimes deep in the call stack.…
Vitaly
  • 411
  • 1
  • 5
  • 11
3
votes
5 answers

Is there a functional difference between initializing singleton in a getInstance() method, or in the instance variable definition

Is there any functional difference between these two ways of implementing a Singleton? public class MySingleton { private static MySingleton instance; public static MySingleton getInstance() { if (instance == null) { …
lbenedetto
  • 2,022
  • 1
  • 21
  • 39
3
votes
3 answers

NoClassDefFound Error while calling Java singleton class in kotlin Class

how I'll be able to get data from Singleton class of java in kotlin class.? since my project is an old project and whole app is in java now I'm implementing its new module in kotlin where I'm using Java's Singleton (CurrentUser.java) in kotlin's…
Atif AbbAsi
  • 5,633
  • 7
  • 26
  • 47
3
votes
2 answers

Why the default constructor executed before the static constructor?

I am wondering why my static constructor is outputting default constructor Static Constructor, and not the other way around Static Constructor and Default constructor or just Default constructor. When I use a static constructor, it should execute…
3
votes
2 answers

Boost python export singleton

I have a singleton (from boost::serialization): class LogManager : public boost::serialization::singleton { ... }; And wrapper for getting instance: inline LogManager &logManager() { return LogManager::get_mutable_instance(); } What's…
Max Frai
  • 61,946
  • 78
  • 197
  • 306
3
votes
2 answers

Add methods from modules to specific instances of a class dynamically

Here's the deal: I need to extend specifica instances of the class Box with some methods. The methods i need to include live inside modules and i want the Box instance to be able to include the modules dynamically. Now i am using a hook with an…
Lucas d. Prim
  • 787
  • 1
  • 5
  • 17
3
votes
1 answer

Thread pool connection vs Singleton design pattern to get a single database connection

I am really getting confused between these 2 now as below : 1. is the returning of only a Singleton instance to a db connection during the entire run time of a JAVA app 2. is the concept of Thread pool connections in general... I mean if we are…
3
votes
1 answer

Does Application instance exist after starting any component?

Should I be sure that if any of my application component is started (onCreate for Activity/Service, onReceived for BroadcastReceiver, etc) then my application instance of Application class already exists? I have static field "instance" in my…
Solvek
  • 5,158
  • 5
  • 41
  • 64
3
votes
3 answers

Best design pattern for objects where state is important - Singleton or Static

More specifically, What's the best approach for classes where state matters, within an application which implements Dependency Injection. Say I need access to an object that's in a particular state. For example, this object might have been initiated…
andy
  • 8,775
  • 13
  • 77
  • 122
3
votes
5 answers

Calling a method of a Ruby Singleton without the reference of 'instance'

I would like to call a method of a Singleton Object without the reference to its instance SingletonKlass.my_method instead of SingletonKlass.instance.my_method i've came up with this solution (using method_missing in the class): require…
ALoR
  • 4,904
  • 2
  • 23
  • 25
3
votes
4 answers

C++: What is the proper way of organising a programs subsystems?

Imagine you have a large application project, the code is split into a range of subsystems implemented as classes derived from CBaseSubsystem. Here already comes my first question: Is it a good idea to organize subsystems as classes [derived from a…
3
votes
1 answer

Thread safety, static methods and some weird code

I recently stumbled upon piece of code similar to the one below. This code does reek right off. Looks like singleton but its not because there is no private constructor. I know for sure this is going to have thread safety issue given big enough load…
ringadingding
  • 475
  • 5
  • 14
3
votes
1 answer

angular - share modal across all components

I'm trying to implement custom confirm dialog functionality which is planned to be available app-wide - across all components. For that I use Angular Material. The modal is a separate component which I resolve in another component in the following…
3
votes
1 answer

Singleton Implementation using Function

I read about multiple approaches of creating a singleton function in this SO question. I came up with another approach def my_singleton(): if not my_singleton.instance: class MyClass: pass my_singleton.instance =…
himanshu219
  • 654
  • 7
  • 22