Home  /  Questions  /  Question



203   96.8
Mar 11, 2010


Singleton usage examples?

Can anyone summarize when and where one would (and would not) use Singletons?
I am asking because it is said that programmers are overusing Singletons....



130   96.7
Mar 19, 2010
Normally i don't use the word single instance throughout the whole application. I rather prefer saying context. For example recently in another post i gave example of Shopping basket per user. Multiple baskets but just one basket per user. The same instance will be returned against the same user. Many people may scream at this definition ;)


Now why overusing ?
First it's very easy to implement. Secondly, being a single instance, it's very easy to be synchronized in mutltithreaded application. Thirdly it solves the toughest of the problem ---- State management in stateless environments.
Consider the same cart being returned for the same user, having the items stored in it, across multiple pages.

There can be many other perspectives to look at this, hopefully over time people will add theirs, this is mine.