Back to list
Views:   12.6K
Replies:  3

Regarding Singleton Design Pattern


I have a question regarding Singleton Design pattern.

When I went through some tutorials, it was mentioned that Singleton insures that only a single instance of a class is returned.

But is it per user basis or application wide?
Harshad Riswadkar, Sep 27, 2010
Reply 1
Speaking strictly from a language standpoint, by default, the Singleton instance is unique to each application process if you are using it within a typical desktop or console application. This is because the instance that is created is marked with as "static". There are threading techniques that can be used to make multiple instances that are unique to their originating thread (as may be done in a web server context), but typically Singleton is used to ensure that all callers get the same instance.
Greg Lusk, May 31, 2011
Reply 2
To add to this, by nature Singleton returns single instance and is meant to provide a global point of access to resources.  This being its main intent there may be variations of this wherein you can have single instance per application, per user, per role or other criteria.  But I am not sure whether that will be called Singleton or any other pattern already exists which solves this problem.

But yeah any pattern can be adopted to the applications need, as long as the core intent remains the same otherwise you create a new pattern and if enough support exists, may be, that can form part of the design patterns catalog :)

Rajesh Pillai, Sep 28, 2010
Reply 3

It depends on the situation and environment. For eg. MS Word which is a desktop application, creates a singleton object per user ie each user logged on to windows user account can open one instance of MS Word.

While in a web application, if an object is implemented as an Application Level object and singleton, then only one instance of that object will be created until the application lives.

While, if your application is an online store and allows each seller to have a customized store, and the implementation of the store is a singleton object, to avoid a seller creating multiple stores, then the singleton instance will be created for each user.

I hope it helps.
Sameer Goyal, Sep 28, 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.