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.
145 questions
-2
votes
1
answer
82
views
Are there any good and modern approaches, to have variables that feel global (no need to pass them around explicitly), yet are local to an instance?
I'm developing a library in JavaScript (TypeScript, actually) which is split into several modules. It's meant to run both on the web and in non-web environments like Node.js.
The library is meant to ...
0
votes
1
answer
594
views
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 ...
0
votes
2
answers
301
views
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 ...
0
votes
2
answers
824
views
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 ...
19
votes
6
answers
9k
views
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 ...
1
vote
4
answers
1k
views
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 (...
1
vote
3
answers
1k
views
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....
1
vote
2
answers
532
views
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():
......
0
votes
4
answers
4k
views
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 ...
-1
votes
3
answers
976
views
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 ...
3
votes
5
answers
7k
views
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 ...
-2
votes
3
answers
1k
views
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 ...
0
votes
1
answer
130
views
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 ...
2
votes
0
answers
350
views
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 ...
1
vote
2
answers
2k
views
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 ...