When and where to use the Singleton Pattern?

 
59   94.7
Feb 01, 2012
 
Hi

Though I've a fair idea of the Singleton design pattern, still I'm at a fix in what scenarios it can be used in a web application.
Can somebody provide some real world web example please.

Thanks
Irshad



280   99.8
Feb 28, 2012
As you were asking about a real world example, here we go.

A few days ago, I had to implement holding a list of loaded assemblies and its references during program-execution. As this is a bigger project, we're talking about 150 dll-files to look at. In that case, I just didn't want to load the same data again and again whenever I need to get one of these assemblies. Therefore, I used a singleton-class, which creates this certain list on its first call and holds that values until the application gets closed.

66   95.6
Mar 20, 2012
Hi, We used the singleton pattern in our utility layers which consist of Loggging, Caching, Service host repositories, Load Balancer... If the question is on how we arrived to the design. There was a performance lag on the utility layer eg, Logging, on diagnosing we observed that there are several instance getting created which are not required in my case. So we adopted Singleton pattern. Hope this helps

60   95.3
Mar 19, 2012
In c# a static class cannot implement an interface. When a single instance class needs to implement an interface for a business contracts or IoC purposes, this is where I use the Singleton pattern without a static class.
 2 comments
 
Perfectly right I think. This is what the reason I was looking for to go for singleton as compared to simple static class. Also I think Singleton provides a way to maintain state in stateless scenarios. --- Sajjad Ahmad  Apr 18, 2012
 
For IocPurposes -> Inherently Singleton advocates private constructor which is required for most of the IoC. --- Tilak Raj  Apr 04, 2013

60   95.3
Mar 26, 2012
Hi

 We can make use of singleton in following scenarios.

1. When the class is stateless eg: Validator classes instead of using static methods for validation because we cant mock static methods so testing becomes difficult. instead if we have singleton we can mock the instance itself.
2. When  we need only one state of class at any given point of time. In this case we have to take care of synchronization.

Thanks
ChandraShekar
 1 comment
 
and for taking care for synchronization we can use the Volatile and Lock. --- Sajjad Ahmad  Apr 18, 2012

50   50
Jun 22, 2012
You don't.

Singleton pattern is an anti-pattern. Please lookup why e.g. http://www.google.com/search?rlz=1C1MDNC_daDK434DK434&sugexp=chrome,mod=5&sourceid=chrome&ie=UTF-8&q=anti+pattern+singleton 

The best alternative I have found is to have a static class that registers managers by their interface e.g.
ISomeKindOfManager someKindOfManager = InstanceRegistry.Get<ISomeKindOfManager>();

Here is more information http://yoawsconsult.blogspot.dk/2009/06/feel-bliss-of-design-pattern.html.

Kind Regards,

Keld Ølykke

 



50   50
Jun 27, 2012
Hi,

We have our DB framework that makes connections to Back end.To Avoid Dirty reads across Multiple users we have used singleton pattern to ensure we have single instance available at any point of time.

Hope that helps you..

50   50
Nov 15, 2013
logging, communication,Configuration classes reading from class [may be put in cache for performance hit] and Database access.

50   50
Jan 27, 2014
I'd say avoid singleton pattern if you can. There are many ways to perform logging/security ect. You could use AOP techniques. The problem with the singleton patter is that its hard to test, it breaks the single responsibility pattern too.

They cause tightly coupled code
They're in control of their own lifecycle
You can mock or test them easily
You cannot extend them (open/closed principle)

I'm just saying really have a think about the design and whether a singleton is the right way to go.

50   50
Feb 09, 2012
Hi,

You can use it in remoting when you want to create a shareable service....

50   50
Feb 09, 2012
The singleton pattern can be used for anything that you don't want to repeat. If the object in question is not expected to change, it is a good candidate for the singleton pattern. The singleton pattern is preferred over a static implementation in most cases. Static implementations can cause dependency headaches. Can you provide more specifics as to what exactly you're looking to do?
 1 comment
 
&quot;Static implementations can cause dependency headaches. &quot;..... Can you tell me how static implementations can cause dependency headaches as compared to Singleton. I donot think this is a valid reason for going for singleton. --- Sajjad Ahmad  Apr 18, 2012

50   50
Feb 10, 2012

50   50
Mar 19, 2012
Logger Instance