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
-3 votes
1 answer
52 views

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 ...
Blue Nebula's user avatar
0 votes
1 answer
593 views

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
2 answers
300 views

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,219
0 votes
2 answers
823 views

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
9k views

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
1k views

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
3 answers
1k views

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
529 views

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
4k views

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
975 views

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
  • 541
3 votes
5 answers
7k views

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
  • 541
-2 votes
3 answers
1k views

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
129 views

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
349 views

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

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

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
  • 71
1 vote
1 answer
150 views

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

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
244 views

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
3k views

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
243 views

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
130 views

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

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
213 views

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,236
1 vote
2 answers
3k views

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
137 views

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
402 views

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
118 views

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
291 views

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
192 views

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
277 views

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
4 votes
3 answers
7k views

So basically I am writing a wrapper for a REST API in objective-c so that our customer can easily use them in their iOS development. I am trying to find a good design pattern for this purpose, it ...
Patroclus's user avatar
  • 169
1 vote
4 answers
4k views

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

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
490 views

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
1k views

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

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
577 views

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

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
122 views

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
991 views

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
247 views

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
502 views

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 architecture ...
Pins's user avatar
  • 201
7 votes
1 answer
19k views

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
147 views

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

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
155 views

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
336 views

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
143 views

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

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
  • 151