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.
138
questions
1
vote
1
answer
361
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
143
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
959
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
498
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
2k
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
492
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
109
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
272
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
1k
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 ...
2
votes
2
answers
649
views
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
...
1
vote
1
answer
119
views
Can I use singleton in a Flyweight factory and still be a valid GoF implementation?
For teaching purposes, I am trying to create a "something useful" example of Flyweight pattern using PHP.
My idea was load some "intrinsic" data from a csv (link) to a pool and ...
4
votes
2
answers
3k
views
What's wrong with using a Singleton?
I'm working on a Python application in which there are two Singleton classes: App and Configuration.
The former seems straight forward, only ever instantiate one App instance; the latter seems ...
1
vote
1
answer
197
views
Alternatives to service locator with opaque pointer in C
I have a project in which one module keeps the state of the target device (things like current command level, but mostly status registers caches).
I'm aware that having a global public variable (...
-3
votes
3
answers
1k
views
Better way than singleton pattern to ensure single instance of class
When this question has been asked before on StackOverflow in 2011 and 2015, all answers as of now suggest to use a Singleton.
But that’s not right.
Singletons are defined by the Gang of Four to ...
-3
votes
1
answer
200
views
What kind of factory method abstract singleton design pattern is this?
I made a code that seems to mix Singleton design pattern, and Fatory method. But my factory method is in an abstract class inherited by my Singleton ... what the hell have I created ? Does it have a ...
2
votes
0
answers
91
views
Convert singleton to single instance with concrete Go example
I have been using singletons in the past.
However, reading articles like Singletons are Pathological Liars have led me to explore alternatives to singletons.
There are a few posts discussion ...
0
votes
2
answers
2k
views
How to separate data and logic to achieve Single Responsibility Principle in C# way? [duplicate]
I'm still learning C# and best practices around it.
Consider this block of code
public class Counter {
private int _value = 0;
public void SetValue(int x) {
_value = x;
// ...
0
votes
1
answer
177
views
What form of single instance class for a cache under Android
The app is to communicate with a bluetooth device with assorted data (config, measurements, service logs, status telemetry) - and it should cache obtained data so the user flipping between activities ...
1
vote
2
answers
2k
views
What is the difference between singleton pattern using inner static "Holder" class and "Instance" variable
I have encountered two patterns to make a singleton class
Holder class
public class Singleton {
private static final class Holder {
private static final Singleton INSTANCE = new Singleton(...
0
votes
1
answer
112
views
Is singleton use acceptable for static single responsibility?
I have been reading into possible design patterns and have found the use of singletons always referred to as an anti-pattern.
I am currently using a singleton for the sole purpose of gathering ...
0
votes
0
answers
382
views
Why do I define my Queries, Data, and Mutation as Singleton when using GraphQL in .NET Core?
Why do I define my Queries, Data, and Mutation as Singleton when using GraphQL in .NET Core?
From the doc's dependency injection page:
public void ConfigureServices(IServiceCollection services)
{
...
2
votes
1
answer
103
views
Is there a name for a "multi-instance singleton pattern" -- returning a reference for a same-valued object?
I am tasked to write an assembler in C, which lends itself sufficiently well in certain design-pattern scenarios, and in a parsing phase, after tokenization, I need to "fill-in" certain information ...
0
votes
2
answers
236
views
Singleton's applicability clarification
According to the GOF Design patterns' book, singleton pattern should be used when:
there must be exactly one instance of a class, and it must be
accessible to clients from a well-known access ...
1
vote
2
answers
134
views
Can I add other methods to the classic Singleton Implementation?
I've seen Singleton design pattern represented like below. Always with just getInstance() method.
My doubt is: once I get the instance of the object I need, if I would like to modify it don't I need ...
0
votes
2
answers
237
views
Is this Singleton-like design pattern a feasible framework to build on?
I am posting this question here after it having been determined to be "off-topic" for stackoverflow, and "too hypothetical" for codereview.
I am experimenting with different singleton-style design ...
1
vote
4
answers
4k
views
IDisposable without finalizer in a Singleton scenario
Is it a right expectation that if a C# class deals with unmanaged resources and implements IDisposable, then it also should implement some kind of finalizer logic?
We have a vendor-supplied utility ...
1
vote
2
answers
1k
views
C# best/cleanest way to do singletons?
I'm working on a small project and I've decided to move over from DI to singleton pattern. Although I know 2 ways to do singleton.
The first one is where every non-model class is a singleton. This ...
1
vote
2
answers
464
views
Handling a limited resource in rest WEB API 2
I have developed a restful WEB API 2 (C#, .NET 4.7) for an internal integration project. In my API's backend, I need to use a vendor's dll in order to process the provided input.
The vendor's dll is ...
3
votes
2
answers
846
views
Singleton for Java Functional Interface
I know there has been 1 million and 1 discussions about Singletons on SO and here and I have had to clean up my fair share of terrible singletons in our code base; one of the reasons I am gun-shy here....
1
vote
2
answers
2k
views
Is it worthy subclassing a Singleton class?
When do we need to subclass a Singleton class or in other words, is it good to subclassing a Singleton?
If its generally allowed what would be the pros and cons and how to handle or get instance from ...
1
vote
1
answer
493
views
Should I use "dynamic" singleton design?
We've created a simple 2D game, it has fields and elements on it, the player can move the elements. The game has two views: a graphical (with images), and a textual (printed to the console). But only ...
1
vote
2
answers
6k
views
Singleton pattern without static
I heard that static (in the Java sense, basically a static method is called on the class itself and not on an instance) is not True OOP. However, how would the Singleton pattern be implemented in such ...
0
votes
1
answer
95
views
Best Approach to have a shared view
I have a website where I want to have a partial view on the page that contains a Singleton service.
I am trying to do this with Razor Pages, and have tried Partial Views and ViewComponents, but ...
1
vote
2
answers
707
views
Can the port of a UML component only be a singleton?
I am working on a software assignment where the design is component based. The components have ports which provide interfaces.
My professor argues that the Port class which is exposed by each ...
-2
votes
1
answer
230
views
Improve design with singleton pattern
I want to create a media management tool, for which I try to draw an appropriate UML class diagram. The tool shall hold a collection of different media types (movies, music, etc.) which inherit from a ...
1
vote
2
answers
415
views
Overhead of using big singleton with all files includes
I have several subsystem managers for various uses, for example:
AudioManager
CollisionManager
InputManager
etc.
At first I wanted them to be all singletons, however now want to make the ...
7
votes
1
answer
18k
views
Singleton with dependency injection ,is that a good practice in some cases?
It's a good idea to have a Singleton implementation with dependency injection?
I have some classes that performs some heavy tasks on instantiation (related to database loading).
I would like to use ...
-1
votes
4
answers
133
views
Base global system for two customizable subcomponents
The following explanation is rather detailed, but I think if I simplified the problem at hand no proper answer could be given.
I'm working on a scripting system for a game (for end users rather than ...
9
votes
3
answers
14k
views
React Native - Is using a singleton the best alternative to DI?
I've been reading a lot about the singleton pattern and how it's "bad" because it makes the classes using it hard to test so it should be avoided. I've read some articles explaining how the singleton ...
-5
votes
1
answer
140
views
Trying to understand singleton objects in Swift [duplicate]
In Swift, if I have a class with only type properties and methods (everything declared static) would that be considered a singleton object? If not, why?
3
votes
1
answer
301
views
If a class has no instance specific data is it okay to make it a singleton?
Let's say I have the following Java code:
public class ObjectConsumer implements Consumer<Object> {
@Override
public void accept(Object o) {
System.out.println("Accepted " + o);
...
-1
votes
1
answer
107
views
How should I represent an object whose instances share the same set of member function identifiers, but those identifiers specify different behaviors?
I'm attempting to develop an open-source Python module for modeling task networks for discrete event simulation. The most fundamental component is the task object, which includes various data such as ...
1
vote
2
answers
2k
views
Creating a singleton to access static data
I have some properties in a property file that I referenced in a singleton so that I can access them easily from different parts of my application. For each property, I put the properties along with ...
2
votes
3
answers
2k
views
Inheritance with singleton
I have come up with the following design for my requirement and want know if there is a better way to do it or if you have any feedback on the design.
Requirement
Assume that an app provides ...
1
vote
1
answer
131
views
De facto weak reference centralized object storage
In a PHP CLI software, we are facing serious memory leakage due to a lot of rotten code, both accumulated in our own software and third-party plugins for our software.
Currently, the object ...
0
votes
3
answers
4k
views
Is a singleton with an update method and software state verification a valid pattern?
Stack overflow suggested that this question my be better suited here. I normally work in c#, but I'm working in a vb.net net application currently. So feel free to respond with vb.Net or c#...
...
1
vote
3
answers
819
views
Should a singleton be used in the following case?
Building an android app that displays a list of of Dog objects in Activity A. When you a tap a Dog in the list, that single dog is accessed by Activity B, Fragment B, and 3 other classes.
After that ...
5
votes
1
answer
2k
views
Why shouldn't a static class have an internal state?
While working on a project, I decided to create a database class to manage my DB connection. I started looking for the best practice to do that, which is usually either a static class or a singleton ...
1
vote
2
answers
346
views
Is singleton the right way to go in the case of a game?
I've been reading the top posts of stackoverflow and SE and all over the place it says how bad singletons are but I am unsure how to rewrite my code.
As of now I have two projects that bind into a ...
1
vote
1
answer
2k
views
Why singleton is an anti-pattern? [duplicate]
Many articles in Internet say that singleton is an anti-pattern, because it makes debugging more difficult.
However I don't understand why debugging a program with a singleton object is difficult. ...