*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes FBN about singleton and single instance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "FBN about singleton and single instance" Watch "FBN about singleton and single instance" New topic
Author

FBN about singleton and single instance

mao lao
Greenhorn

Joined: Nov 26, 2002
Posts: 26
Hi, guys?
I searched singleton on this forum
some people say use singleton on Data and LockManager and passed
others say not good to use singleton, also passed
but I am not so clear about this, if I use RMI, after init the instance of the RemotDataImpl and bound it to the server, the application only have one instance and it init only one LockManager instance, it is in fact a singleton even not use sinagleton pattern to do it.( for this assignment only).
So is there any difference between using or not using singleton?
It is acturally only one instance, even there are so many clients use it, is that right, I am really confusing about this.
Thanks
[ August 30, 2003: Message edited by: mao lao ]
[ August 30, 2003: Message edited by: mao lao ]
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11481
    
  94

Hi Mao,
if I use RMI, after init the instance of the RemotDataImpl and bound it to the server, the application only have one instance and it init only one LockManager instance

This is the way that I (and others) think you should be doing this.
But this is not a singleton. Someone could come along and modify your code in the future, and have more than one instance of those classes.
A singleton enforces that only one instance of the class can ever be created. It handles this internally (by making the constructor private, and creating a getter method to return the one instance that the class itself instantiates).
What you are doing is having a controlled number of instances of these classes. This is the "Multiton" pattern.
So is there any difference between using or not using singleton?

Because a singleton ensures that there can only ever be one instance of your class, it limits future expandability. For example, at present you have a Data class and a LockManager class. Is there anything in those classes that stops them from being used for a different database? If not, then as long as you have not forced them to be Singletons, you could reuse the same code for another data file at the same time as you are using them for your current data file. If they were singletons though, you would not be able to use them, and would have to write new code for any extra data files.
If you search for "Peter den Haan" and "Singleton" in this forum, you will find some comments about the overuse of singletons, (and some interesting poetry from Eugene ).
Regards, Andrew
[ August 31, 2003: Message edited by: Andrew Monkhouse ]

The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
mao lao
Greenhorn

Joined: Nov 26, 2002
Posts: 26
Thanks.
I read some post, someone said because of using singleton, so it is very easy to handle multi-client, what is that mean?
I think even not using singleton, because in fact it is only one instance, using synchronized method is enough to handle multi-client, am I missing something?
[ August 31, 2003: Message edited by: mao lao ]
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11481
    
  94

Hi Mao,
someone said because of using singleton, so it is very easy to handle multi-client, what is that mean?

I'm sorry, but I don't know what they meant. Perhaps if you had a link to the original post we might be able to comment on it.
Personally I cannot see any advantage for using a Singleton over a single instance for the Data class. For both of them you have to ensure your methods are thread safe.
Regards, Andrew
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: FBN about singleton and single instance