Questions tagged [singleton]
The singleton is a design pattern aiming to ensure that only a single instance of a class can be created and used.
143 questions
0votes
1answer
493views
Global Variables State Management
Background: I am working in a Java environment using Spring Boot, where I often encounter scenarios where global variable state management is critical, especially within singleton services. I have ...
0votes
3answers
230views
Are there any benefits to delegation over subclassing/inheritance in the case of a singleton?
Example: The MacOS API known as Cocoa uses Delegation to specify various behaviors within the app. A Delegate is a separate object that implements a set of methods that are called by an original ...
0votes
2answers
603views
Is it considered a code smell to not delete static pointers of a singleton?
I have a singleton that needs to be initialized at the start of the program and it will live on until the end of the program. As is usual in this pattern, a function initializing the singleton creates ...
19votes
6answers
8kviews
Is utilizing a singleton for a cache an antipattern?
I'm currently writing an MVC application and I need a class that can: A: get, add and remove data(specifically a TreeSet of sorted strings that I want stored in memory, but I doubt the data itself is ...
1vote
4answers
1kviews
Should I use a singleton to represent the application as a whole?
I make sure when designing software/firmware to make heavy use of dependency injection so that different pieces of the application are not directly coupled to one another. This also allows me to (...
1vote
3answers
986views
Storing multiple instances on a Singleton?
RefactoringGuru's example Singleton in Python has an _instances dictionary field class Singleton(type): _instances = {} def __call__(cls, *args, **kwargs): if cls not in cls....
1vote
2answers
413views
Using class attributes as globals in Python - is there a catch?
I have found myself in the habit of using code like this. class glb: "just for holding globals" args = None # from argparse conf = None # from configparser def main(): ......
0votes
4answers
3kviews
Should I design a factory that returns a singleton?
I design a common API for selected printers of different brands in Java. Each printer uses a different underlying SDK with different functions, but any hardware my code runs on will have only one ...
-1votes
3answers
872views
Would Injecting dependencies in C# as default parameters be a bad practice?
Given the (old) debate over whether Singletons are overused/abused/are worth it - would it be a bad idea to inject the dependencies as default parameters? In this way, we could get rid of defining ...
3votes
5answers
6kviews
How do Singletons differ from Static variables?
Although I do find some (apparently old) posts on the topic on the web, I could not find one here at SE. Thought of raising this here to see if what I read is accurate/is all there is to it. So ...
-2votes
3answers
904views
Singleton as Interface for testability via dependancy injection
It can be found in many advices on topic that having Singletons is an anti-pattern. Especially for cases of testability. Can someone please advice/critique on this way (please see code below) of ...
0votes
1answer
115views
How should I provide access to "global" objects down to other objects which are contained in a central "application" object
Note This is a bit lengthy to have give a better understanding of the situation and to get some context. You might spot other architectural flaws (it's from an ancient application). I appreciate any ...
2votes
0answers
330views
Dependency injection in .NET Core 3.x Worker Services: Why so hard to consume Transient services now?
I have a Repository service that should be Transient. It is used in many applications. I have a new application that's a Console App, and current guidance suggests implementing my business logic in ...
1vote
2answers
2kviews
Best way to access (grand grand) parent element in gui
I'm creating a multi window gui program, in c++ with Qt Widgets. I do have many custom gui elements, which usually are c++ classes inherited from QWidget or other Qt elements. When foo is the main ...
2votes
2answers
1kviews
Is it right way to make Window to Singleton and call Visibility and Shutdown() methods directly in WPF?
There is a parent window that is the basis of the program, and there are several sub windows under it. Editor is one of the sub window and I'm making it. The code is as follows: // This is Code-Behind ...