Skip to main content

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.

Filter by
Sorted by
Tagged with
0 votes
1 answer
377 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 ...
procrastinator1771's user avatar
0 votes
3 answers
210 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 ...
CPlus's user avatar
  • 1,189
0 votes
2 answers
469 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 ...
Reverent Lapwing's user avatar
19 votes
6 answers
8k 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 ...
Tyler Del Rosario's user avatar
1 vote
4 answers
913 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 (...
Patrick Wright's user avatar
1 vote
1 answer
812 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....
Michael Moreno's user avatar
1 vote
2 answers
374 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(): ......
Alias_Knagg's user avatar
0 votes
4 answers
3k 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 ...
Martin Braun's user avatar
-1 votes
3 answers
785 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 ...
Veverke's user avatar
  • 471
3 votes
5 answers
5k 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 ...
Veverke's user avatar
  • 471
-2 votes
3 answers
827 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 ...
psb's user avatar
  • 101
0 votes
1 answer
115 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 ...
exhuma's user avatar
  • 1,211
2 votes
0 answers
319 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 ...
catfood's user avatar
  • 1,615
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 ...
Turtle10000's user avatar
2 votes
2 answers
1k 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 ...
wddfrwd's user avatar
  • 61
1 vote
1 answer
137 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 ...
celsowm's user avatar
  • 253
4 votes
2 answers
4k 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 ...
pstatix's user avatar
  • 1,047
1 vote
1 answer
214 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 (...
Martel's user avatar
  • 615
-3 votes
3 answers
2k 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 ...
John Doe's user avatar
-3 votes
1 answer
225 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 ...
Motiss's user avatar
  • 1
2 votes
0 answers
119 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 ...
peaxol's user avatar
  • 121
0 votes
2 answers
3k 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; // ...
Kevin Tanudjaja's user avatar
0 votes
1 answer
209 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 ...
SF.'s user avatar
  • 5,176
1 vote
2 answers
3k 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(...
Harish Rana's user avatar
0 votes
1 answer
123 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 ...
Necro's user avatar
  • 105
0 votes
0 answers
386 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) { ...
Jose A's user avatar
  • 305
2 votes
1 answer
106 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 ...
Acsor's user avatar
  • 201
0 votes
2 answers
255 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 ...
isqo's user avatar
  • 242
1 vote
2 answers
168 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 ...
Maicake's user avatar
  • 225
0 votes
2 answers
252 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 ...
user avatar
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 ...
Tamás Somogyi's user avatar
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 ...
Navine's user avatar
  • 31
1 vote
2 answers
479 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 ...
Ahmet's user avatar
  • 11
3 votes
2 answers
970 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....
Hangman4358's user avatar
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 ...
Sazzad Hissain Khan's user avatar
1 vote
1 answer
563 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 ...
klenium's user avatar
  • 121
1 vote
2 answers
7k 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 ...
vikarjramun's user avatar
0 votes
1 answer
101 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 ...
Isaac Levin's user avatar
1 vote
2 answers
918 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 ...
patrick246's user avatar
-2 votes
1 answer
235 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 ...
Jannek S.'s user avatar
  • 121
1 vote
2 answers
482 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 ...
Pins's user avatar
  • 201
7 votes
1 answer
19k 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 ...
X.Otano's user avatar
  • 622
-1 votes
4 answers
135 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 ...
edcruz's user avatar
  • 1
9 votes
3 answers
15k 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 ...
Mateo Hrastnik's user avatar
-5 votes
1 answer
143 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?
Joe's user avatar
  • 101
3 votes
1 answer
311 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); ...
Mibac's user avatar
  • 262
-1 votes
1 answer
126 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 ...
mcman's user avatar
  • 1
1 vote
2 answers
3k 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 ...
qbolt's user avatar
  • 51
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 ...
Sudar's user avatar
  • 123
1 vote
1 answer
134 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 ...
SOFe's user avatar
  • 728