The tag has no usage guidance.

learn more… | top users | synonyms

1
vote
1answer
155 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 ...
0
votes
0answers
97 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 ...
4
votes
2answers
612 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
0answers
13 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 ...
7
votes
3answers
448 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 ...
40
votes
11answers
3k 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 ...
6
votes
2answers
446 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
258 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
150 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, ...
4
votes
3answers
236 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 ...
4
votes
3answers
252 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 ...
2
votes
1answer
389 views

Applying initialization-on-demand holder idiom at variable level rather than class

I was looking for the idiomatic way to implement thread-safe lazy initialization for a configuration collection retrieved from the DB inside a Spring bean. I decided to adapt the initialisation-on-...
14
votes
4answers
3k 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
4answers
401 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 ...
3
votes
2answers
613 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 ...
1
vote
1answer
115 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
493 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 ...
2
votes
1answer
270 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
1answer
453 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
169 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
111 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
273 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
92 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
783 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 { ...
14
votes
3answers
2k views

Could a singleton type replace static methods and classes? [duplicate]

Possible Duplicate: What is the difference between all-static-methods and applying a singleton pattern? In C# Static methods has long served a purpose allowing us to call them without ...
1
vote
2answers
234 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, ...
1
vote
1answer
431 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
151 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
261 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
242 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
2k views

const vs. readonly for a singleton

I have a singleton that anchors together some different data structures. Part of what I expose through that singleton are some lists and other objects, which represent the necessary keys or columns ...
2
votes
1answer
192 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
3answers
2k 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
170 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
221 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
519 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 = ...
50
votes
5answers
5k views

When is Singleton appropriate? [closed]

Some hold that the Singleton Pattern is always an anti-pattern. What do you think?
2
votes
2answers
261 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
685 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
450 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 so,...
13
votes
3answers
4k 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 ...
1
vote
6answers
2k views

Static classes and/or singletons — How many does it take to become a code smell? [closed]

In my projects I use quite a lot of static classes. These are usually classes that naturally seem to fit into a single-instance type of thing. Many times I use static classes and recently I've started ...
1
vote
3answers
163 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 ...
0
votes
1answer
240 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 ...
6
votes
3answers
5k 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
1k 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
2k 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
924 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 out-...
17
votes
3answers
18k 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 ...
18
votes
5answers
12k views

What is the difference between all-static-methods and applying a singleton pattern? [duplicate]

I am making a database to store information about the users of my website (I am using stuts2 and hence Java EE technology). For the database I'll be making a DBManager. Should I apply singleton ...