• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

FBN about singleton and single instance

 
mao lao
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 11865
194
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
mao lao
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 11865
194
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic