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.

The singleton is a design pattern aiming to ensure that only a single instance of a class can be created and used.

138 questions
610
votes
13 answers

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, there are still many cases where I can't see a…
71
votes
5 answers

When is Singleton appropriate?

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 some valid use cases for it?
Fishtoaster
  • 25,909
  • 15
  • 111
  • 154
44
votes
11 answers

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 break the SOLID principle?
Den
  • 4,827
  • 2
  • 32
  • 48
38
votes
3 answers

When to use a Singleton and when to use a static class

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 use a static class. (In languages which don't…
Aviv Cohn
  • 21,190
  • 31
  • 118
  • 178
34
votes
9 answers

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 use singletons is when I need a factory (let's say…
Giorgio
  • 19,486
  • 16
  • 84
  • 135
31
votes
5 answers

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

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 pattern here or rather make all its methods static? I…
shahensha
  • 599
  • 1
  • 6
  • 15
28
votes
3 answers

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 static Foo createFooerFoo() {...} } During a code…
bstempi
  • 457
  • 1
  • 4
  • 11
26
votes
7 answers

The Singleton Pattern

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 Singleton. However, looking at some comments on…
Darren Young
  • 2,175
  • 1
  • 20
  • 27
20
votes
4 answers

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 method to make the Singleton pattern…
logeeks
  • 339
  • 1
  • 3
  • 6
16
votes
3 answers

Could a singleton type replace static methods and classes?

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 instantiating classes. Only in later year have we became…
Homde
  • 11,104
  • 3
  • 40
  • 68
16
votes
3 answers

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 library if they decided to use a database…
dabadaba
  • 2,216
  • 6
  • 25
  • 35
15
votes
4 answers

Singleton or instantiate everytime I use?

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 whether to set up that class as a singleton, or just…
bebbi
  • 361
  • 3
  • 8
14
votes
3 answers

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 Foo1(){} public void doo(){ ... } } With…
irreputable
  • 657
  • 5
  • 7
13
votes
5 answers

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 assembly. He didn't like that I was created a new…
Darren Young
  • 2,175
  • 1
  • 20
  • 27
13
votes
4 answers

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/NSNotificationCenter_Class/Reference/Reference.html Usually…
Magnus Wolffelt
  • 2,373
  • 2
  • 20
  • 23
1
2 3
9 10