Dofactory.com
Dofactory.com
 Back to list
Views:   27K
Replies:  6
Archived

Singleton Pattern & its usage

Hi,

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.

Thanks,
Harshad Riswadkar, Mar 29, 2010
Reply 1

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.

Sreenivas Manyam Rajaram, Apr 23, 2010
Reply 2
Hi,
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.

--
Kaushal
http://kaushalp.blogspot.com
Kaushal Patel, Apr 14, 2010
Reply 3
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.


Cheers,
Hamid
Hamid Ghader, Apr 08, 2010
Reply 4
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
The Saint, Apr 08, 2010
Reply 5
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

Sriram Krishnan, Apr 05, 2010
Reply 6
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
http://www.dotnetpal.com/pattern/singleton.htm
Ashish Sharma, Mar 31, 2010
Stay Inspired!
Join other developers and designers who have already signed up for our mailing list.
Terms     Privacy     Cookies       Do Not Sell       Licensing      
Made with    in Austin, Texas.  - vsn 44.0.0
© Data & Object Factory, LLC.