• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

singleton vs multiton data object

 
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am writing my choices.txt. It just reflected to my mind that: is this an issue of using a singleton vs multiton data object in this project?

Singleton vs multiton is a big subject in this project and many did use singleton pattern for this project. However, it seems it should not matter. The instruction says that we can assume that there is only one access to the physical data file at any time. So why should we bother here? I can further deduce that there is only one server is up and running against the database file, even though multiple connection can concurrently coming in to this server and thus the only one data object. The assumption is: if you have only one database file handler open and open persistently. otherwise, one has to make sure that the file handlers are not modifying the same records in the multiton case. I guess this is the reason many adopted singleton.

However, in the situation I mentioned and implemented, it should not matter. May I be wrong? Please give me some comments.
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Andy,

I always thought never use singleton unless it realy needed.

Personally, I don’t think singleton should be used for the data class, for the following reason.

Our data class can handle one data file at time, but what if in the future you need to create another data file (contains customer info for example), if you are using a singleton class then you can’t do it, else all you have to do is set the file in data constructor.

So I think data file should not be singleton.
 
Ranch Hand
Posts: 1033
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Omar Kalaldeh:
Hi Andy,

I always thought never use singleton unless it realy needed.

Personally, I don’t think singleton should be used for the data class, for the following reason.

Our data class can handle one data file at time, but what if in the future you need to create another data file (contains customer info for example), if you are using a singleton class then you can’t do it, else all you have to do is set the file in data constructor.

So I think data file should not be singleton.



This is an interesting question. I don't know of any overall rule against using the singleton pattern, and multiton is usually more complex.

In these projects a single object is required to access a particular data file as it needs to be able to perform the i/o operations in a race free manner and it needs to coordinate the locking of resources.

If you were supporting a number of files it would make sense to have a factory and a pool of file access objects, keyed by file name. You could use that approach with this project, instead of using a singleton.

This question does raise the further question about the coupling of a lock manager to a file access object. If you wish to support multiple files the lock manager must be bound to a specific file access object.
 
Andy Zhu
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey, Omar: thanks for your comments.
Peter: you made a very good point.

I understand, through the reading of posts in this forum, sun doesn't deduce points for using singleton. But I try to understand how singleton/multiton comes into play in the regime of client/server architecture, given an example of this scjd project using RMI.

As I understand, when server is up, there will be only one copy of data object. when a client connects, the rmi server just allocates a thread for the client with an assigned id. As each thread get its own stack, the client will have its own thread and associated stack. I guess at this time, the client's stack would have either a copy of data object or a pointer to the data object. Or are there any other possibility? Which one is the correct operation by rmi?

Thanks for your help.
 
peter wooster
Ranch Hand
Posts: 1033
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Andy Zhu:
Hey, Omar: thanks for your comments.
Peter: you made a very good point.

I understand, through the reading of posts in this forum, sun doesn't deduce points for using singleton. But I try to understand how singleton/multiton comes into play in the regime of client/server architecture, given an example of this scjd project using RMI.

As I understand, when server is up, there will be only one copy of data object. when a client connects, the rmi server just allocates a thread for the client with an assigned id. As each thread get its own stack, the client will have its own thread and associated stack. I guess at this time, the client's stack would have either a copy of data object or a pointer to the data object. Or are there any other possibility? Which one is the correct operation by rmi?

Thanks for your help.



One thing you should be aware of is that there is no guarantee that a particular RMI client will always be using the same thread. So you need something else to identify the client.

What I do is to have each client have a distinct Data instance that then uses a single DataFile object that is associated with a specific file. The instance of Data is referenced by a DataAdapterImpl which is the implementation of the DataAdapter that is shared over RMI.

If you don't do anything about lost connections this is probably not a problem. What I do is have the DataAdapterImpl implement Unreferenced so I can unlock any orphan locks.
 
Andy Zhu
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey, Peter: thanks for your point. I did take an easy way out.
 
You showed up just in time for the waffles! And this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic