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.
Nov 01, 2015
There are three use cases where we should go for singleton pattern.
1. When a class has no attribute ,so it has no state and we are passing value as method parameters, in that case there is no need to create multiple objects . By creating single object we can call that methods multiple times . See below for the example:
public area (int radius)
// here i am creating object for single time if i create multiple object also it refer to the same state only. So here no need to create multiple objects ,so declare a private constructor to make singleton class.
Circle c=new Circle();
One more point also you can rise here that if we declare the methods as static then no need to create object.I can agree with this point but if we declare as static then there is a chance in the application we can create object but if we make the class as singleton then there is no chance that we can create more than one object in entire application.
2. When the case is read-only state then at that case also there is no need to create multiple objects ,we can declare singleton.
private final pi=3.1412;
In this case also declare class as singleton because here if we create multiple objects and call methods then it is also refer to the same object.
I hope this will helpful ,i have only given the idea ....so if anything wrong reply
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?