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.
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
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.
Mar 26, 2012
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.
Jun 27, 2012
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..
Nov 15, 2013
logging, communication,Configuration classes reading from class [may be put in cache for performance hit] and Database access.
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.
Feb 09, 2012
You can use it in remoting when you want to create a shareable service....
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?