This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes singleton vs multiton data object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "singleton vs multiton data object" Watch "singleton vs multiton data object" New topic
Author

singleton vs multiton data object

Andy Zhu
Ranch Hand

Joined: May 26, 2004
Posts: 145
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.


--------<br />Andy Zhu<br />scjp 1.4<br />scjd 1.4<br />SAS Certified Programmer 9.0
Omar Kalaldeh
Ranch Hand

Joined: Jun 08, 2004
Posts: 58
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.


Best Regards <br /> <br />Omar F. Kalaldah<br />---------------------<br />SCJP, SCJD, SCBCD
peter wooster
Ranch Hand

Joined: Jun 13, 2004
Posts: 1033
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

Joined: May 26, 2004
Posts: 145
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

Joined: Jun 13, 2004
Posts: 1033
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

Joined: May 26, 2004
Posts: 145
hey, Peter: thanks for your point. I did take an easy way out.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: singleton vs multiton data object
 
Similar Threads
Why use cookie and strange locking scheme
revisit role of locking and the level of fineness for singleton
NX: Database vs Data / MetaData
URLyBird - Question about Singleton pattern
A Question In The Monkhouse Book