Home  /  Questions  /  Question

60   95.3
Mar 29, 2010

Singleton Pattern & its usage


I am new to this forum and the topics of Design pattern. I am interested in know where the Singleton pattern is applicable and where it is useful.


50   50
Mar 31, 2010
Singleton pattern is used where you need to create only single instance for whole application. Lets take example of session counter of site I will create only single object of session counter and change counters when user create session or close it. No need to create new object in this case. here I will use singleton design pattern for creating object. For singleton class example in .net you can check link below

50   50
Apr 05, 2010
Well this can extend on to a single instance of the application as opposed to single instance of a class. In those cases you may have to use classes like Mutex and Registry to achieve your objective

50   50
Apr 08, 2010
I would use singleton only, if the usage is some kind of stateless - otherwise you will have problems with testing (thats why singleton and static sometimes can suck if they are used in the wrong context
 1 comment
The Google Singleton Detector, or GSD, is a tool which analyzes Java bytecode and detects the use of Singletons. It detects four different types of global state, including singletons, hingletons, mingletons and fingletons, to allow you to see all of the uses of global state inside a project and remove them.... to help testing http://code.google.com/p/google-singleton-detector/ --- Pari Rohani  Apr 09, 2010

50   50
Apr 08, 2010
I used Singleton to open just one winform at a same time via a menu. When user clicks the menu item again, if the form was already open, it doesn't create another form and just display the current one.


85   96.4
Apr 14, 2010
The constructor of the class gets execute only ONCE in the lifecycle. Then the single instance can serve all the request. One can easilt implement this using LOCK.


Apr 23, 2010

Recently I used a Singleton Pattern in one of my projects. Find the excerpt of the constraints as below.

I had to develop a Windows service to download data from a Mainframe database for individual entities and create MS Access files from these. I had only read access to the AS400. Also if the Windows services is restarted, it needed to continue from where it left off. So, I needed a datastore to keep track of my Windows service processing.

I was specifically instructed that I could not use SQL Server and had to use a  custom XML database as it was for a smaller purpose. Also the Windows service had to be multi threaded to download the AS400 data for multiple entities parallely.

Since I needed to use an XML datastore which was accessed concurrently by multiple threads, I could have only one instance of my XML file in memory at any given point of time.

Therefore, I decided to use the Singleton pattern here. In addition, we used locks and synchonizing objects within the Singleton.