Last call to make your voice heard! Our 2022 Developer Survey closes in less than a week. Take survey.

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
2 answers
130 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 ...
user avatar
  • 295
1 vote
5 answers
300 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 ...
user avatar
  • 295
-2 votes
3 answers
97 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 ...
user avatar
  • 1
0 votes
1 answer
100 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 ...
user avatar
  • 1,191
2 votes
0 answers
141 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 ...
user avatar
  • 1,611
1 vote
1 answer
499 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 ...
user avatar
2 votes
2 answers
216 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 ...
user avatar
  • 61
1 vote
1 answer
87 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 ...
user avatar
  • 243
4 votes
2 answers
2k 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 ...
user avatar
  • 297
1 vote
1 answer
155 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 (...
user avatar
  • 605
-3 votes
3 answers
810 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 ...
user avatar
-3 votes
1 answer
158 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 ...
user avatar
  • 1
2 votes
0 answers
80 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 ...
user avatar
  • 121
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; // ...
user avatar
0 votes
1 answer
125 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 ...
user avatar
  • 5,068
0 votes
2 answers
1k 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(...
user avatar
0 votes
1 answer
107 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 ...
user avatar
  • 5
0 votes
0 answers
327 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) { ...
user avatar
  • 275
2 votes
1 answer
98 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 ...
user avatar
  • 201
0 votes
2 answers
208 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 ...
user avatar
  • 122
1 vote
2 answers
119 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 ...
user avatar
  • 225
0 votes
2 answers
214 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
3k 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 ...
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 ...
user avatar
  • 31
1 vote
2 answers
427 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 ...
user avatar
  • 11
3 votes
2 answers
684 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....
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 ...
user avatar
1 vote
1 answer
421 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 ...
user avatar
  • 121
1 vote
2 answers
5k 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 ...
user avatar
0 votes
1 answer
94 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 ...
user avatar
1 vote
2 answers
508 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 ...
user avatar
-2 votes
1 answer
222 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 ...
user avatar
  • 121
1 vote
2 answers
362 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 ...
user avatar
  • 201
7 votes
1 answer
16k 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 ...
user avatar
  • 592
-1 votes
4 answers
129 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 ...
user avatar
  • 1
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 ...
user avatar
-5 votes
1 answer
138 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?
user avatar
  • 101
3 votes
1 answer
289 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); ...
user avatar
  • 262
-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 ...
user avatar
  • 1
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 ...
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 ...
user avatar
  • 123
1 vote
1 answer
126 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 ...
user avatar
  • 618
0 votes
3 answers
3k 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#... ...
user avatar
1 vote
3 answers
772 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 ...
user avatar
  • 139
5 votes
1 answer
1k 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 ...
user avatar
1 vote
2 answers
328 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 ...
user avatar
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. ...
user avatar
  • 722
3 votes
2 answers
9k views

Using singletons in Python (Django)

I was suggested to define a singleton in Python in the following way: class Controller: pass # ... controller = Controller() However, this way, controller cannot be replaced with a derived ...
user avatar
  • 722
2 votes
1 answer
1k views

Do singletons use encapsulation?

I'm reading the Design patterns book by the Gang of 4. Does a singleton use encapsulation? To me it would seem like yes, but i'm not 100% sure. Maybe I don't have a good understanding of ...
user avatar
-4 votes
2 answers
437 views

Can we fix Singletons testability tight coupling?

It is well known that Singletons are anti-patters for several reasons: tight coupling singletons are pathological liars no testability inheritance is not available you cannot program to interfaces, ...
user avatar
  • 1,073