Questions tagged [singleton]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
-3
votes
3answers
199 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
1answer
113 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 ...
564
votes
13answers
232k views

So Singletons are bad, then what?

There has been a lot of discussion lately about the problems with using (and overusing) Singletons. I've been one of those people earlier in my career too. I can see what the problem is now, and yet, ...
1
vote
0answers
38 views

Convert singleton to single instance with concrete Go example

I have been using singletons in the past. However, reading articles like http://misko.hevery.com/2008/08/17/singletons-are-pathological-liars/ have led me to explore alternatives to singletons. ...
0
votes
2answers
387 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; // ...
1
vote
2answers
939 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 ...
0
votes
1answer
29 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 ...
33
votes
3answers
53k views

When to use a Singleton and when to use a static class [duplicate]

I've searched about this here and on StackOverflow and found some differences between the two. But I'm still not sure in what cases one would prefer a Singleton, and in what cases one would choose to ...
1
vote
2answers
2k 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
6answers
3k views

How to change the state of a singleton in runtime

Consider I am going to write a simple file based logger AppLogger to be used in my apps, ideally it should be a singleton so I can call it via public class AppLogger { public static String file = ...
0
votes
2answers
253 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
1answer
84 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
0answers
153 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) { ...
0
votes
1answer
75 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 ...
8
votes
3answers
10k 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 ...
2
votes
1answer
89 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
2answers
130 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 ...
0
votes
2answers
108 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 ...
2
votes
1answer
919 views

Where to keep persistent services in MVVM architecture?

In an MVVM application I need to use some services that are persistent, e.g. network services like P2P and WCF where the main "engine" or server endpoint must remain up and running during the lifetime ...
0
votes
2answers
189 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
4answers
2k 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
2answers
795 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 ...
0
votes
2answers
311 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
2answers
365 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
votes
4answers
128 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 ...
1
vote
1answer
288 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
2answers
218 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 ...
1
vote
1answer
116 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 ...
3
votes
4answers
3k views

Is the singleton pattern prone to thread safety problems?

Is the singleton pattern prone to thread safety problems? If so, what are the best methods to work around them?
3
votes
1answer
320 views

Efficient Code Design for Pattern Search Using a Singleton Pattern

I am having difficulty figuring out an efficient (thread-safe) code design for the following problem. I have been at it for some time now and would really appreciate some advice and input on how best ...
1
vote
2answers
272 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 ...
-2
votes
1answer
216 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 ...
3
votes
2answers
2k views

Is it reasonable to use a decorator to instantiate a singleton in Python?

Suppose I have a class like this: class Foo(object): # some code here As it happens, Foo is a singleton. There are numerous ways to write singletons in Python, but most of them don't really ...
5
votes
1answer
9k 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 ...
-5
votes
1answer
136 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
1answer
215 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); ...
12
votes
4answers
5k views

Are immutable/stateless singletons bad?

Lately there have been some kind of revolution against singletons, but is there something wrong with them if they are stateless? I know the overuse talk and all... this applies to everything not just ...
-1
votes
1answer
102 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 ...
2
votes
1answer
6k 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 ...
9
votes
2answers
2k views

How to avoid a crazy amount of interfaces in UI with dependency injection?

Problem I recently read a lot about Singletons being bad and how dependency injection (which I understand as "using interfaces") is better. When I implemented part of this with callbacks/interfaces/DI ...
1
vote
2answers
1k 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
3answers
1k 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 ...
0
votes
3answers
1k 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
3answers
680 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 ...
1
vote
2answers
314 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 ...
4
votes
1answer
995 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 ...
3
votes
2answers
170 views

Does a serializable singleton imply both value and reference semantics at the same time?

I was reading Effective Java, and I came across passages that talk about ways you might implement a serializable singleton, as if this was a perfectly normal thing to do in Java. This immediately ...
-2
votes
2answers
802 views

C++ Which of the following is a better singleton pattern approach?

I'm now stuck in the server I am programming, simply because I do not seem to find a good and realiable way to handle my "single object" classes, I've attempted the Singleton pattern but it is just ...
1
vote
1answer
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. ...
2
votes
1answer
903 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 ...