Singleton discussion

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Dec 2007
Posts: 1,718
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 230
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Singleton discussion

 
0
  #1
Oct 12th, 2009
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:
I was told that it is better to a have the class as singleton, than have the methods static
Do you agree or not?
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 1,030
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 152
JamesCherrill JamesCherrill is offline Offline
Veteran Poster
 
0
  #2
Oct 12th, 2009
It's common practice in the Java API to make "utility" methods static, so that makes it OK for me.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 14
Reputation: msi_333 is an unknown quantity at this point 
Solved Threads: 1
msi_333's Avatar
msi_333 msi_333 is offline Offline
Newbie Poster
 
0
  #3
Oct 12th, 2009
static java methods is a bit slower than default methods .!
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 1,030
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 152
JamesCherrill JamesCherrill is offline Offline
Veteran Poster
 
0
  #4
Oct 12th, 2009
Originally Posted by msi_333 View Post
static java methods is a bit slower than default methods .!
That's interesting. Do you have a source for that information?
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,718
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 230
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso
 
0
  #5
Oct 12th, 2009
Originally Posted by msi_333 View Post
static java methods is a bit slower than default methods .!
I've found in another forum someone saying the opposite, although I believe he was referring to C#.
Where did you get that information?
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 114
Reputation: ejosiah is an unknown quantity at this point 
Solved Threads: 12
ejosiah's Avatar
ejosiah ejosiah is offline Offline
Junior Poster
 
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
Knowledge is power but love conquers all
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,718
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 230
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso
 
0
  #7
Oct 12th, 2009
Originally Posted by ejosiah View Post
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
Well I also agree with you. If a class will have only static methods then the constructor should be declared private so no one can instantiate it.
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
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 114
Reputation: ejosiah is an unknown quantity at this point 
Solved Threads: 12
ejosiah's Avatar
ejosiah ejosiah is offline Offline
Junior Poster
 
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,657
Reputation: BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold 
Solved Threads: 206
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso
 
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,718
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 230
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso
 
0
  #10
Oct 13th, 2009
Originally Posted by ejosiah View Post
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
Actually the article I was referring said exactly that. The factory class I mentioned is to be used to create more than one class, but one instance of each class.



Originally Posted by ejosiah View Post
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).
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
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 478 | Replies: 12
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC