| | |
Singleton discussion
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
I encountered some code at work that got me thinking.
There was a util - class that had some methods that had nothing to do with the state of the class. They were all "util" methods. Meaning that they could all be declared static with no problem because the only thing they did was some calculations and return the result.
But methods were declared non-static and the class was created as singleton. So even though an instance was needed to call them, because the class was singleton only one instance was created in the end.
I asked the programmer why the methods weren't declared static, and I was told that it is better to a have the class as singleton, than have the methods static.
I search the net for an answer:
http://forums.sun.com/thread.jspa?th...art=0&tstart=0
http://blogs.msdn.com/scottdensmore/...25/140827.aspx
http://www.ibm.com/developerworks/we...co-single.html
And the conclusion that i came is that it depends on the way you want to use the singleton class. Sometime it is better and sometimes you should avoid it depending on the implementation and how you want to use it.
Now I believe that the methods should be static in the case I described. What I would like, is a response about this comment:
Do you agree or not?
There was a util - class that had some methods that had nothing to do with the state of the class. They were all "util" methods. Meaning that they could all be declared static with no problem because the only thing they did was some calculations and return the result.
But methods were declared non-static and the class was created as singleton. So even though an instance was needed to call them, because the class was singleton only one instance was created in the end.
I asked the programmer why the methods weren't declared static, and I was told that it is better to a have the class as singleton, than have the methods static.
I search the net for an answer:
http://forums.sun.com/thread.jspa?th...art=0&tstart=0
http://blogs.msdn.com/scottdensmore/...25/140827.aspx
http://www.ibm.com/developerworks/we...co-single.html
And the conclusion that i came is that it depends on the way you want to use the singleton class. Sometime it is better and sometimes you should avoid it depending on the implementation and how you want to use it.
Now I believe that the methods should be static in the case I described. What I would like, is a response about this comment:
•
•
•
•
I was told that it is better to a have the class as singleton, than have the methods static
Last edited by javaAddict; Oct 12th, 2009 at 5:35 am.
Check out my New Bike at my Public Profile at the "About Me" tab
0
#3 Oct 12th, 2009
static java methods is a bit slower than default methods .!
Just walk beside me and be my friend
Programming forums,phpbb3 Styles,
Java Forums,Web Development Forums
Programming forums,phpbb3 Styles,
Java Forums,Web Development Forums
0
#6 Oct 12th, 2009
I don't agree. let me give you the definition of static members and Singleton classes from a design point of view. static members are those that exist independent of every instances of that class. thus you would only use static members if they result to the same thing or same action for every instance of that class. Singleton objects exist because for every other object in the same context there should exist only one of it self.
I guess you already know this. its a programming flaw to to create a utility class and also make it possible to create instances of it. You should always prevent this by making its constructor private and provide no way of access an instance of such class (utility classes are only for convenience and should follow strict design rules).
If an instance is needed then it should not be a utility class
I guess you already know this. its a programming flaw to to create a utility class and also make it possible to create instances of it. You should always prevent this by making its constructor private and provide no way of access an instance of such class (utility classes are only for convenience and should follow strict design rules).
If an instance is needed then it should not be a utility class
Knowledge is power but love conquers all
0
#7 Oct 12th, 2009
•
•
•
•
I don't agree. let me give you the definition of static members and Singleton classes from a design point of view. static members are those that exist independent of every instances of that class. thus you would only use static members if they result to the same thing or same action for every instance of that class. Singleton objects exist because for every other object in the same context there should exist only one of it self.
I guess you already know this. its a programming flaw to to create a utility class and also make it possible to create instances of it. You should always prevent this by making its constructor private and provide no way of access an instance of such class (utility classes are only for convenience and should follow strict design rules).
If an instance is needed then it should not be a utility class
Of course my concern was, is it better instead of having static methods to have a singleton class. In that way it will also be impossible to have many instances.
In one of the links I read, and I agree with it, was that you should not create your class to be as singleton, but if you want it to be initialized once then create a factory class with a method that will create and return only one instance.
In that way you don't limit others to use the class in any way they might see fit.
Check out my New Bike at my Public Profile at the "About Me" tab
0
#8 Oct 12th, 2009
creating a factory class just to instantiate a singleton object is redundant. factory classes are there to ease the creation of several classes not one. the best we to access a singleton object is via a factory method defined by the class itself i.e. getInstance. (see GOF design patterns for more info).
Knowledge is power but love conquers all
•
•
Join Date: Sep 2008
Posts: 1,629
Reputation:
Solved Threads: 205
0
#9 Oct 12th, 2009
I agree with you, addict. I found an interesting note on wikipedia as well: "Another notable difference is that static member classes cannot implement an interface, unless that interface is simply a marker. So if the class has to realize a contract expressed by an interface, it really has to be a singleton."
Out.
0
#10 Oct 13th, 2009
•
•
•
•
creating a factory class just to instantiate a singleton object is redundant. factory classes are there to ease the creation of several classes not one
So if we combine our ideas, the class would have a public constructor, but also a static method capable of creating and returning only one instance of the class; the programmer is free to follow any path they choose depending on their goal
Check out my New Bike at my Public Profile at the "About Me" tab
![]() |
Similar Threads
- Singleton class Worked example (C++)
- singleton class with example....... (C++)
- Stamp Exchange and Discussion Forum [ PR4 / DMOZ / Yahoo ... ] (Websites for Sale)
- Technical Resume review discussion (IT Professionals' Lounge)
- Cheap Sitewide link on Popular Webmaster Discussion Forum! (Ad Space for Sale)
- Making visitors participate in discussion! (Social Media and Online Communities)
- Discussion Board (JSP)
Other Threads in the Java Forum
- Previous Thread: storing object into array
- Next Thread: Create code for Binary Search Tree using compareTo method using I/O
| Thread Tools | Search this Thread |
Tag cloud for Java
addressbook android api apple applet application arguments array arrays automation awt binary bluetooth button calculator chat class classes client code columns component converter database draw eclipse error errors event exception file fractal ftp game givemetehcodez graphics gridlayout gui helpwithhomework html ide image inetaddress input integer invokingapacheantprogrammatically j2me java javaprojects jme jmf jni jpanel julia link linux list loop map method methods midlethttpconnection mobile netbeans newbie number objects openjavafx oracle php print problem program programming project projects recursion rim scanner screen server set signing size smart sms socket sort sql storm string support swing test threads time tree variablebinding webservices windows






