Dofactory.com
Dofactory.com
 Back to list
Views:   192.1K
Replies:  14
Archived

When and where to use the Singleton Pattern?

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
Irshad Ahmad, Feb 01, 2012
Reply 1
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:
   
       class Circle{
         
          public area (int radius)
             {
                //display area
                 area=3.1412*r*r;
             }
        }


// 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();
        c.area();

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.
         Ex:
             class Circle 
              {
                  private final pi=3.1412;
                 public area()
                 {
                       //display area
                  }
             } 

       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
Vedprakash Panda, Nov 01, 2015
Reply 2
Singleton pattern restricts the instantiation of a class and ensures that only one instance of the class exists in the java virtual machine. The singleton class must provide a global access point to get the instance of the class. 

 Singleton pattern is used for logging, drivers objects, caching and thread pool.(in log4j,Hibernate we are using the this pattern)

For more Details about Singleton Design Pattern then go through this blog http://adnjavainterview.blogspot.in/2014/06/singleton-design-pattern-in-java-with.html
Anil Nivargi, Jun 21, 2014
Reply 3
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.
Anthony Joanes, Jan 27, 2014
What is the purpose of the FeeManager? If it is to calculate Fees for students, may I suggest that you instead add a new method to the IStudentManager called CalculateFee() ?
Apr 16, 2011
Reply 4
logging, communication,Configuration classes reading from class [may be put in cache for performance hit] and Database access.
Arun Prakash, Nov 15, 2013
Reply 5
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..
Rk Muddala, Jun 27, 2012
Reply 6
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

 


Keld Ølykke, Jun 22, 2012
Reply 7
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
Chandrashekar M, Mar 26, 2012
Reply 8
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
Pavan Kumar Kota, Mar 20, 2012
Reply 9
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.
Sean Mackey, Mar 19, 2012
Reply 10
Logger Instance
Nick Karnik, Mar 19, 2012
Reply 11
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.
Florian Schaeffler, Feb 28, 2012
Reply 12
Rrr R, Feb 10, 2012
Reply 13
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?
Benjamin Moore, Feb 09, 2012
Reply 14
Hi,

You can use it in remoting when you want to create a shareable service....
Krishn Pras, Feb 09, 2012
Thanks Robert I got your point.. but what if I want to implement the different fuctionality in each manager. I mean In studentManager I may do some basic DML operation but in Fee Manager I need more functions to handle transactions.. so what u suggest for this to handle in best way..
Apr 15, 2011
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.