Home  /  Questions  /  Question



256   99.6
Sep 27, 2010


Regarding Singleton Design Pattern

Hi,

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?



70   96.1
Sep 28, 2010
Hi,

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.

1,364   100.0
Sep 28, 2010
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 :)




50   50
May 31, 2011
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.