The singleton tag has no usage guidance.
0
votes
0answers
19 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
185 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
1answer
56 views
Best practice for defining and keeping related stateless methods 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 ...
1
vote
1answer
91 views
HashMap to replace Singletons?
Consider the following: I have controllers and views in a client-application. As this runs purely on the client side, each controller must only exist once.
At first I thought about implementing ...
1
vote
2answers
120 views
Joshua Bloch Enum Singleton and Third Party APIs
In the book Effective Java he give the best Singleton pattern implementation in his, that is implement by a Enum. I have doubt to how to implement this pattern with a third party API.
I'm using an ...
0
votes
1answer
119 views
Is having a Data Access Layer as singleton a good ideea
I'm working on a project(web application) where I have a DataManager(IDALContract dalImplementation) class that does some stuff with the data after retrieving it from the database or before saving it ...
0
votes
3answers
112 views
How should I refactor a singleton (to be used by a container) when the refactored class requires initialization and takes a dependency?
I'm refactoring an old app to use dependency injection.n I'm pretty new to DI.
I have a class that used to be a singleton. I'm refactoring it into a non-singleton class and using the container to ...
3
votes
1answer
101 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 ...
0
votes
0answers
40 views
What sort of design pattern should I be using here - Tabs Generation
I am fairly new with design patterns, I have been studying them but no practical experience so this is going to be my first try, Graphical representation of GUI is shown in this diagram,
Points
...
2
votes
2answers
264 views
Is this the solution to static inheritance?
I've seen a lot of posts recently on why Singletons should be avoided. However, I can't see any of those problems with the following solution to a common problem: static inheritance.
For example, I ...
0
votes
0answers
51 views
Will this singleton style class work with accessing a session on multiple MVC controllers?
Question background:
I have an e-commerce site I'm developing that requires multiple controller methods to access a single session variable.
The idea:
My idea was to create a singleton style class ...
3
votes
2answers
228 views
Factory for creating a singleton instance
We have some legacy code that has a bunch of singletons all over the place (written in C#).
The singleton is a fairly "classic" implementation of the pattern:
public class SomeSingleton
{
...
2
votes
1answer
139 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 ...
1
vote
2answers
137 views
Is storing data that rarely changes in-memory a good candidate for a singleton/cache?
I have a class called DataPoint that is defined like the following. Currently, it goes through each property on an object and based on the DataPoint, it does some formatting like padding, trimming, ...
0
votes
1answer
111 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 ...
0
votes
1answer
137 views
Why use the Singleton pattern over class functions and fields? [duplicate]
I'm going to start by saying that I understand that programming in mostly class functions and variables can be harmful to object-orientation, and that most of the time an instance is preferred. I'll ...
0
votes
1answer
253 views
How could I avoid singletons in this case? [duplicate]
Using Dart in the client part of a web app where I have an application manager that contains a List of application instances and references to services that are used by each application as in the ...
2
votes
1answer
186 views
Why use a enum to create the singleton pattern [duplicate]
Why would you use an enum to create a singleton pattern?
To what purpose would it serve over a conventional singleton pattern?
I have seen the above used. The code uses an enum to create this pattern ...
2
votes
1answer
170 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 ...
0
votes
0answers
34 views
Designing Document class for Doc Imaging - optional fields
I'm working on a project that processes many Documents.
A Document CAN have a Cover Sheet (if the first page is one)
A Document CAN have a Stub (if Document is large)
It matters whether or not a ...
1
vote
3answers
438 views
Why does dependency injection encourage collaboration to be exposed via constructors?
The general approach to DI that I see in answers like
So Singletons are bad, then what? encourages
business objects that collaborate with other objects to (a) not
directly create those instances and ...
1
vote
0answers
109 views
Using Singletons in JS with workers
I'm building a javascript application that does continuous drawing, but relies on asynchronous scripts in the background which also need to poll for events and write to "global variables". This got me ...
1
vote
1answer
181 views
Do you use static keyword to declare single instance pattern? [duplicate]
I just come across this code and find it interesting to know whether you consider this as another single instance pattern?
public class Initiator
{
private static String str;
// there is many ...
0
votes
3answers
257 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 = ...
1
vote
2answers
269 views
Is this a candidate for Singleton?
I have an external thermometer connected via USB that is controlled by my SW. Different parts of my system will use it but never at the same time (all in one thread). However, it is a single device ...
1
vote
2answers
170 views
Passing central objects around or having global instances? [duplicate]
Having a larger WinForms application with several classes I currently pass references to several "central" objects around to function calls.
This leads to more method parameters.
Example:
public ...
3
votes
3answers
470 views
Infinite loop with a singleton - does this type of issue have a name?
I ran into an unusual error while working on my project. To better learn from and remember it, I'd like to know if this type of error has a name or some definition. (The error itself OutOfMemoryError ...
0
votes
2answers
319 views
Java Singleton fields implementation
I have singleton in Java and I have realized, that I could make its fields static and it would work same way as regular instance fields.
Would there be a performance / optimization difference? If ...
0
votes
0answers
327 views
Use a global variable, a singleton, or something else
Preface: I am working in PHP (Abandon hope all ye who enter here).
Background: There exists a large set of global functions in PHP, a number of which are miscellaneous system calls, like sleep (and ...
0
votes
1answer
189 views
Singleton: Why is a global point of access bad? [duplicate]
Most people here and on StackOverflow agree that Singletons are evil.
The main explanation I've come across against the use of Singletons, is they fact that they provide a global point of access to ...
1
vote
3answers
148 views
Simple Design for Singleton function in java for Kernel [closed]
I understand that Singelton helps to instantiate only one class AT A TIME. I try to learn how to Design for Singleton function in java. I want to know it better to understand Kernel. So I try to do ...
2
votes
3answers
833 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 ...
2
votes
1answer
957 views
MVC design, singleton in model with initialization
I've recently learned of MVC (model view controller) and am trying to refactor an existing program. I am in a situation where I'd like to have exactly one object of a particular class so it seemed ...
0
votes
1answer
945 views
Using module level declared global “singletons” in python
Ok, I know that using singletons is generally a bad practice, but if I do it (for db connection, logging et al.) am I allowed to go (in respect of clean design) with a module defined variable that is ...
0
votes
2answers
697 views
Difference between Singleton pattern and auto_ptr<> resp. unique_ptr<>
I'm maintaining some legacy code of a physical simulation. The calculation object is build as a singleton, to ensure there is only one instance. A co-worker told me, that singleton are completely ...
16
votes
3answers
11k views
Static factory vs factory as a singleton
In some of my code, I have a static factory similar to this:
public class SomeFactory {
// Static class
private SomeFactory() {...}
public static Foo createFoo() {...}
public ...
-1
votes
3answers
510 views
Is it possible to encapsulate the object itself within a dynamic property?
I ask this question out of curiosity.
Lets say the class Foo is a singleton and I want to call Foo instead of Foo.Instance to access the static object.
Would it be possible to encapsulate the ...
1
vote
4answers
2k views
Is it allowed to make multiple instances of a singleton class?
I am confused when I read this (regarding singleton design pattern):
How do we ensure that a class has only one instance and that the instance is easily
accessible? A global variable makes an ...
7
votes
2answers
394 views
How to maintain synchronised state across threads - is this a good use for a Singleton?
I hear a lot about "Singletons are always bad" around the place. I don't hate on them to that degree but I try not to use them if I have a better alternative.
In this case I have a system that ...
3
votes
2answers
429 views
Application Settings, Status and more as Singleton? [duplicate]
I am relatively new to programming (1yr field experience now) and I think that I am overusing the singleton pattern and I need some input on how to do it in a better way. Right now I am using ...
5
votes
2answers
254 views
How to avoid a singleton in this example?
I have a class Image and another one ImageStore. The Image it's just a value object containing the package name and the image name. The ImageStore does all the actual work: calculates the real path of ...
3
votes
2answers
426 views
Is there a name for this design pattern? [closed]
I don't like singletons, but I have to compromise now (but only as temporary measure).
However I don't want to go completely singleton. I'd rather want to use this pattern:
interface ...
3
votes
3answers
639 views
Why don't we use a class and its static members to do what a singleton does?
In Java,C++,Objective C, C#, and all other similar OOP languages,
Why don't we use a class with static methods and vars instead of going through a singleton syntax having to deal with various ...
5
votes
4answers
1k views
Is there something like a polymorphic Singleton?
I have a resource loader object that loads objects from the disk or from a ZIP archive, depending on the platform.
It has state (e.g. the path to the directory or the ZIP archive, and possibly some ...
1
vote
1answer
88 views
How to get a reference to a central AssetLoader in every Widget?
I'm working on a simple UI system for a game. The building blocks are Widget objects, which can contain each other. There are several sub classes of Widget, e.g. LabelWidget, ImageWidgetand ...
5
votes
4answers
2k 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 ...
5
votes
3answers
2k views
How to create a manager class without global variables nor singletons?
I would like to implement some kind of manager class in my application. It will be in charge of loading textures, processing them, distributing them etc...
At first, I wanted to make a global ...
12
votes
3answers
3k views
What are the downsides of implementing a singleton with Java's enum?
Traditionally, a singleton is usually implemented as
public class Foo1
{
private static final Foo1 INSTANCE = new Foo1();
public static Foo1 getInstance(){ return INSTANCE; }
private ...
0
votes
5answers
583 views
should singleton be life-time available or should it be destroyable?
Should the singleton be designed so that it can be created and destroyed at any time in program or should it be created so that it is available in life-time of program. Which one is best practice? ...
3
votes
2answers
1k views
null values vs “empty” singleton for optional fields
First of all I'm developing a parser for an XML-based format for 3D graphics called XGL.
But this question can be applied to any situation when you have fields in your class that are optional i.e. the ...