Skip to main content

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
2 votes
1 answer
4k 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 ...
Babyburger's user avatar
6 votes
1 answer
7k 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 ...
zeller's user avatar
  • 161
3 votes
2 answers
2k 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-...
Smii's user avatar
  • 51
28 votes
3 answers
46k 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 ...
bstempi's user avatar
  • 457
-1 votes
3 answers
2k 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 ...
icosamuel's user avatar
  • 113
-1 votes
4 answers
8k 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 ...
joey rohan's user avatar
7 votes
2 answers
497 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 ...
glenatron's user avatar
  • 8,699
4 votes
2 answers
2k 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 ...
metacircle's user avatar
5 votes
2 answers
297 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 ...
Totty.js's user avatar
  • 170
3 votes
2 answers
478 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 ...
d_inevitable's user avatar
5 votes
3 answers
2k 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 ...
Amogh Talpallikar's user avatar
6 votes
4 answers
4k 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 ...
futlib's user avatar
  • 2,157
1 vote
1 answer
100 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 ...
futlib's user avatar
  • 2,157
12 votes
6 answers
7k 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 ...
Random42's user avatar
  • 10.5k
7 votes
3 answers
5k 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 ...
Saturn's user avatar
  • 3,907
14 votes
3 answers
10k 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 ...
irreputable's user avatar
0 votes
5 answers
2k 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? ...
Manoj R's user avatar
  • 4,076
4 votes
2 answers
4k 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 ...
Uko's user avatar
  • 231
1 vote
3 answers
361 views

Name for Osherove's modified singleton pattern?

I'm pretty well sold on the "singletons are evil" line of thought. Nevertheless, there are limited occurrences when you want to limit the creation of an object. Roy Osherove advises, If you're ...
Kazark's user avatar
  • 1,820
3 votes
4 answers
4k 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?
DeveloperDon's user avatar
  • 4,978
0 votes
2 answers
413 views

Template rendering engine legitimate use of a singleton?

I wrote a standalone singleton class (scaffold) tonight that serves as a facade to a few other classes, including a template class and a view class. Templates and views are both named and instances ...
Calvin Froedge's user avatar
1 vote
5 answers
1k views

Does "Property" in C# cost much?

I have a class created in some namespace, like SomeNameSpace.SubNameSpace.StaticClassName Here is a code snippet from other code file where I want to use this class many more times, so I created a ...
Sreekumar P's user avatar
-1 votes
1 answer
2k views

What is the difference between class level attributes, Class constants and singleton methods?

What the difference given they all exist 'once per class'. When should I use one over another, i.e. for what purpose? I use Ruby.
Michael Durrant's user avatar
3 votes
1 answer
4k 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 ...
user avatar
35 votes
10 answers
32k views

Alternatives to the singleton pattern

I have read different opinions about the singleton pattern. Some maintain that it should be avoided at all costs and others that it can be be useful in certain situations. One situation in which I ...
Giorgio's user avatar
  • 19.7k
12 votes
7 answers
17k views

What are the roles of singletons, abstract classes and interfaces?

I am studying OOP in C++ and, even though I am aware of the definitions of these 3 concepts, I cannot really realize when or how to use it. Let's use this class for the example: class Person{ ...
appoll's user avatar
  • 353
7 votes
4 answers
4k views

C++ - Best way to have a central data repository

I'm coding in C++. I have a server that will have clients connect to it, and each client gets spun off into its own thread for communication. This server is hosting a game, so there is information ...
will's user avatar
  • 173
4 votes
1 answer
2k views

DAO/Webservice Consumption in Web Application

I am currently working on converting a "legacy" web-based (Coldfusion) application from single data source (MSSQL database) to multi-tier OOP. In my current system there is a read/write database with ...
Gavin's user avatar
  • 43
5 votes
3 answers
12k views

Static class vs Singleton class in C# [duplicate]

Possible Duplicate: What is the difference between all-static-methods and applying a singleton pattern? I need to make a decision for a project I'm working of whether to use static or singleton. ...
radu florescu's user avatar
20 votes
4 answers
21k views

Dependency Injection and Singleton. Are they two entirely different concepts?

I've been hearing about using the dependency injection over Singleton for my colleague. I still can't make out if it they are two orthogonal patterns which can be replaced with one another? Or is DI a ...
logeeks's user avatar
  • 339
2 votes
3 answers
1k views

Memory problems with singletons in a test suite

I am having a memory leak when running a test suite due to singleton objects are created and not cleared after each test case. And these singleton objects are holding references to heavy objects. So ...
Mohamed Ramadan's user avatar
8 votes
4 answers
512 views

Is there is a case when there is no any reasonable replacement for singleton design pattern? [duplicate]

Possible Duplicate: When is Singleton appropriate? I've seen many explanations why is Singleton is evil. But is there really no such a case when Singleton is the only beautiful solution?
eigenein's user avatar
  • 181
0 votes
1 answer
185 views

Is using Singleton correct for Jtree & DefaultTreeModel?

Basically I have one Tree view which uses a single DefaultTreeModel. Is it okay to use singleton for Tree extending JTree and another singleton for TreeModel extending DefaultTreeModel? If not, what ...
Kim Jong Woo's user avatar
2 votes
6 answers
3k 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 ...
Earlz's user avatar
  • 22.9k
16 votes
3 answers
4k 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 ...
Homde's user avatar
  • 11.1k
619 votes
13 answers
271k 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, ...
Bobby Tables's user avatar
  • 20.6k
13 votes
5 answers
1k views

What to say to your boss if they want you to use a global variable

I am currently 4 months into an internship, and when reviewing my code, my boss didn't like that I had kept a specific object local to a number of methods across a few separate classes within one ...
Darren Young's user avatar
  • 2,175
26 votes
7 answers
4k views

The Singleton Pattern [duplicate]

Possible Duplicate: When is Singleton appropriate? I am a new programmer (4 months into my first job) and have recently taken an interest in design patterns. One that I have used recently is the ...
Darren Young's user avatar
  • 2,175
31 votes
5 answers
36k 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 ...
shahensha's user avatar
  • 599
7 votes
2 answers
509 views

Are the default objects of VB a type of singleton?

Are the default objects supported by My keyword in VB.net is a kind of singleton? If not, what is the difference? Again is this "default object" feature is something useful? If yes, then why it is ...
Gulshan's user avatar
  • 9,452
13 votes
4 answers
2k views

Does “notification center” pattern encourage good or bad program design?

Sometimes I come across these message-hub-style APIs, for example the Cocoa NSNotificationCenter: http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/...
Magnus Wolffelt's user avatar
72 votes
5 answers
17k views

When is Singleton appropriate? [duplicate]

There is a widely accepted opinion that Singleton is an anti-pattern. As usual, there are always exceptions to the rule. Can you explain why Singleton is a bad choice in general and give an example of ...
Fishtoaster's user avatar
  • 25.9k

1 2
3