It's not a secret anymore!*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes does the Data class have to be a singleton? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "does the Data class have to be a singleton?" Watch "does the Data class have to be a singleton?" New topic
Author

does the Data class have to be a singleton?

joel smither
Ranch Hand

Joined: Jan 01, 2005
Posts: 31
Hi all, I'm working the Urlybird developer project and I've been reading alot of posts about how people implemented a singleton for the Data class (the class that actually implemented readRecord, updateRecord, etc).

I did not do that in my design and I'm wondering if that's a big mistake. Each client that I spin up calls on a factory to return either a local database connection or a remote one. Each client effectively get's its own connection to the database file, hence each client gets its own copy of the Data class. Is there a problem with that?

any help would be deeply appreciated.
thanks in advance.
Ken Kirin
Greenhorn

Joined: Dec 02, 2004
Posts: 26
Hi Joel,

I didn't apply Singleton pattern to my Data class too. I think it is acceptable if each network client has its own Data object as long as these Data objects use a common object for tracking the data that another client is locking. For me, I have another class called "DatabaseFileManager" which is Singleton and returns the HashMap object that is used to track all locked records. Of course this HashMap object is commonly used by my network clients. We don't lock the Data object but instead we lock this HashMap object, then it's supposed to have only one single HashMap object. Hope this helps.

Regards,
Ken Kirin
SCJP
SWCD
SCJD(B&S In progress)
Eric Chang
Ranch Hand

Joined: Jan 27, 2004
Posts: 113
I chose to make the Data class singleton, but I know people who have chosen not to. As long as you make sure you only have one instance of the actual object that holds the records (unless you aren't bothering with cacheing and always reading from the datafile), then you should be fine.
Tommy Wan
Greenhorn

Joined: Jan 05, 2005
Posts: 14
If I make the Data class as Singleton, Do I still need the sychronization at all in any method. Since only client can hold the Data object which will access the one file operation - read/write/update/delete, why do we need synchornization anyway?
David Abramowicz
Ranch Hand

Joined: Dec 10, 2004
Posts: 56
Originally posted by Tommy Wan:
If I make the Data class as Singleton, Do I still need the sychronization at all in any method. Since only client can hold the Data object which will access the one file operation - read/write/update/delete, why do we need synchornization anyway?


Hi.

Implementing the Data class as a singleton makes sure there is only ONE Data class. It does not make sure that only one client can access the Data class at a time.

That is what you need the synchronization for, to avoid two clients entering the Data class lock operation (for instance) concurrently.

Cheers /Dave
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: does the Data class have to be a singleton?
 
Similar Threads
Singleton problem
NX: URLyBird Data class question
Final check before upload
Passed with 395 points.
Platform Independent Command Line