Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [singleton]

The tag has no usage guidance.

0
votes
1answer
79 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
27 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
1answer
84 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
80 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
104 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
2answers
172 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
763 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
468 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
183 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
199 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....
0
votes
1answer
290 views

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
1answer
199 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
1answer
881 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
1answer
50 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
2answers
155 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
1answer
207 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
2answers
208 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
4k 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
4answers
125 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 ...
8
votes
3answers
7k 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
1answer
128 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
193 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
1answer
96 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
2answers
716 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 ...
3
votes
3answers
897 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
1answer
100 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
3answers
938 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
595 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 ...
4
votes
1answer
667 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
2answers
309 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
1answer
1k 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
4k 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 ...
2
votes
1answer
721 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 ...
-4
votes
2answers
272 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, ...
-2
votes
2answers
653 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 ...
0
votes
4answers
295 views

Singleton Usage

Enviroment: I need to share some kind information (see HashMap) through different part of my code. I think that Singleton could be a perfect design pattern - except for one thing: Information ...
1
vote
1answer
661 views

Loading data from JSON file for one time

I have JSON file which has some hundereds of rows stored like below.. [ { "Id": "1", "Name": "A" }, { "Id": "1", "Name": "A" } ] I am trying to load this into collection of a ...
2
votes
3answers
1k views

Using spring to Avoid Singleton pattern

Considering that Spring framework can manage pretty everything related to object creation (life cycle, object scope ..), is it longer necessary to use singleton pattern in an application using the ...
0
votes
0answers
186 views

A modification of singleton pattern

"Singleton" is widely considered anti-pattern ("bad" in everyday language). What about this alternative modified singleton pattern? An object constructor for our class ensures that at any given ...
1
vote
0answers
202 views

.NET server object instance: Singleton vs Per Call vs Per Client in the context of a 2 tier architecture

Note: This is a theoretical question weighing up the pros and cons of server object instancing, the tier architecture is not the topic of the question. My question is simply whether my thoughts are ...
11
votes
3answers
12k views

Should a DAO be singleton or not?

I am developing a RESTful API and I think it is convenient to use DAOs for my resources because although I plan on just using memory to store them, I don't want to close a door to whoever is using my ...
43
votes
11answers
5k views

Are so called “cross-cutting concerns” a valid excuse to break SOLID/DI/IoC?

My colleagues like to say "logging/caching/etc. is a cross-cutting concern" and then proceed using the corresponding singleton everywhere. Yet they love IoC and DI. Is it really a valid excuse to ...
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 ...
0
votes
2answers
890 views

Without using Singletons, what is a good way to manage a global resource? [duplicate]

Unlike many libraries SDL and OpenGL are designed to provide global resources; you can access them at any time from any class. There are justifications for this: They are written in C, meant to be ...
1
vote
1answer
229 views

Making more than one instance of factory that is supposed to be singleton

However, there are some classes that should have only one instance. Sometimes they are factories, which you can use to create the other objects in the system. If more than one factory exist, ...
5
votes
3answers
3k views

Alternatives to Singletons for caching lists of data?

In my project, I have an abstract Cache class that allows me to populate a series of lists that globally persist throughout my application. These cache objects are thread-safe and can be manipulated ...
14
votes
4answers
4k views

Singleton or instantiate everytime I use? [duplicate]

I use a class that just extracts data from one known object, and distributes it to other known objects. No persistent configuration or such is needed in that class instance. How should I decide ...
1
vote
0answers
423 views

When to use trait components, objects, package objects in Scala

When I am adding dependencies in Scala, I tend to use mixin trait components for those that are inherit to the class itself. For example, DaoGenerator extends Generator However, for trait components ...
3
votes
2answers
3k views

Converting static utility class into singleton

In company where I work we have lots of "utility" classes, each has lots of code inside (thousands of lines), and they are all static. And one static methods call anothers. The problem here is that ...
4
votes
3answers
420 views

How do you define stateless methods while keeping them together in Objective-C?

I have a few methods which are stateless: loginWithEmail: password: completion: signUpWithEmail: password: completion: resetPasswordForUsername: completion: Currently what I do is I create a class ...