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

Set.of vs Collections.singleton

Since the immutable implementation of Set.of(E e) has been introduced in Java 9, do we still need to use Collections.singleton(E e)? What would be the use case for the latter one? It doesn't seem to be obvious from looking at the source code of…
Andremoniy
  • 34,031
  • 20
  • 135
  • 241
3
votes
1 answer

Trying to access resources from an Application Context in Android

I have a singleton that holds a lot of information on my App (ACCU.class). I'm using the application context to do a single initialization and single finishing. One of the key features is another singleton called IMCDefinition that reads from a raw…
JQCorreia
  • 727
  • 6
  • 15
3
votes
4 answers

How to hold collection of files throughout application?

In my mobile application I need to hold a collection of File objects (pictures, documents) that can be accessed throughout the whole application and users can make various operations over the collection: view all/individual files upload subsets of…
Ondřej Z
  • 5,094
  • 3
  • 24
  • 30
3
votes
1 answer

singletons and google coding-style

Google c++ coding-style does not allow non-trivial static objects (and for a reason) and hence no singletons. At the same time singletons do represent reality of application logic. So what is the correct way to implement singleton functionality…
uuu777
  • 765
  • 4
  • 21
3
votes
2 answers

Is it safe to store data in a kotlin object?

I have a question regarding objects in kotlin, to which I couldn't find a satisfying answer so far. It's basically the following scenario: I've got some user data, that I want to make available through the entire app, without having to pass it…
lionthefox
  • 369
  • 1
  • 2
  • 16
3
votes
2 answers

Should I export singleton class for consumption in React?

I have an AuthService class that provides all the api calls and handles authentication for those calls, so its a nice modular service. It is not a React component and not used in any render calls. It handles fetch calls mostly. In many other classes…
Stevie
  • 326
  • 3
  • 16
3
votes
1 answer

Using a singleton-ish approach for node.js modules

I use node-cache in my expressed based application. It needs to be setup using the following code; const NodeCache = require( "node-cache" ); const myCache = new NodeCache( { stdTTL: 100, checkperiod: 120 } ); After that it can be used…
Bastiaan
  • 686
  • 1
  • 8
  • 20
3
votes
2 answers

When using the singleton design pattern, do other methods need to use synchronized keyword to ensure thread safety?

I want to ensure that the following class is thread-safe, should I use the synchronized keyword for other methods? Or use a thread-safe data structure to store Email. What should I do? public class RecycleStation { private static volatile…
xun yan
  • 69
  • 5
3
votes
1 answer

How horrible is Static-Casting Base class to Derived class in order to call destructor in tests (for tests)?

Preface: I am aware that the following is a very dirty hack. We will refactor things (so we can write non-hacky tests, use Mocks and possibly even get rid of the Singletons), but first we need test coverage... As said before, we have a significant…
CharonX
  • 2,130
  • 11
  • 33
3
votes
1 answer

Singleton vs Stateful remote EJB references in clusters

I'm using a vanilla JBossAS 6 server with a couple of projects to test the functionality of clustered JBoss environments. The problem that I have is that if I transfer an EJB reference from one EJB (of another type) in a node, to an instance of the…
3
votes
0 answers

Free functions vs singleton vs static class members

I've inherited some legacy code with lots of global variables as well as lots of global functions using them. I intend to refactor the code into something cleaner, but I see 4 ways of doing it (which I've put below in a simplified form) and I have…
Eternal
  • 2,648
  • 2
  • 15
  • 21
3
votes
1 answer

Can a singleton in spring have static methods?

Component and Service type beans and most of the other bean types are by default singleton. In one of my code Pull requests, I declared a method as static in a Component as the method wasn't modifying any class level variables. My code reviewer…
Vineeth Chitteti
  • 1,454
  • 2
  • 14
  • 30
3
votes
1 answer

Swift Singleton access in Objective-C triggers EXC_BAD_INSTRUCTION (EXC_I386_INVOP)

I've created a singleton object in Swift 4.2 and am attempting to access it (call a couple methods) in an Objective-C class. However, when calling the instance in Objective-C, the app crashes with the following: EXC_BAD_INSTRUCTION…
Sam Spencer
  • 8,492
  • 12
  • 76
  • 133
3
votes
1 answer

Delphi Singleton double check locking

I want to implement a singleton critical section in order to protect some code across several classes(don't ask further...). I've seen Delphi Singleton Pattern and Aquire Singleton class Instance Multithread but these are using a global function to…
RBA
  • 12,337
  • 16
  • 79
  • 126
3
votes
0 answers

React service singleton alternative

I'm making a ReactJS application using the google maps library & I'm trying to abstract most map logic to a service, so that I could exchange the google maps library with leaflet if I wanted to in the future. The way it works now is that I have a…
sjbuysse
  • 3,872
  • 7
  • 25
  • 37