This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes [B&S]:  Using a Singleton class to represent the file resource Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "[B&S]:  Using a Singleton class to represent the file resource" Watch "[B&S]:  Using a Singleton class to represent the file resource" New topic
Author

[B&S]: Using a Singleton class to represent the file resource

Vincent Hernandez
Ranch Hand

Joined: Oct 17, 2004
Posts: 43
Hello fellow ranchers

I was hoping for some feedback on part of my design for the B&S project.

I'm aiming to create a singleton class to represent the file itself. I understand I must create a class sunceritfy.db.Data which implements suncertify.db.DBAccess. However, I want to have some sort of object representation of the file itself. In addition, I don't want to reopen and close the file for every Data instance. The name for this singleton is class ContractorDB.

Here's the main jist of what I am currently implementing:



Multiple instances of Data will be invoked, so I figured having a singleton resource will best suit my needs. As for record locking, I will have a static Vector used to keeps track of the locked Contractor records (no, I wouldn't lock the entire DB!).

Again, any feedback or questions would be appreciated.

Thank you!
[ December 26, 2004: Message edited by: Vincent Hernandez ]
peter wooster
Ranch Hand

Joined: Jun 13, 2004
Posts: 1033
Originally posted by Vincent Hernandez:
Hello fellow ranchers

I was hoping for some feedback on part of my design for the B&S project.

I'm aiming to create a singleton class to represent the file itself. I understand I must create a class sunceritfy.db.Data which implements suncertify.db.DBAccess. However, I want to have some sort of object representation of the file itself. In addition, I don't want to reopen and close the file for every Data instance. The name for this singleton is class ContractorDB.

Here's the main jist of what I am currently implementing:



Multiple instances of Data will be invoked, so I figured having a singleton resource will best suit my needs. As for record locking, I will have a static Vector used to keeps track of the locked Contractor records (no, I wouldn't lock the entire DB!).

Again, any feedback or questions would be appreciated.

Thank you!

[ December 26, 2004: Message edited by: Vincent Hernandez ]


You're on the right track. The database table needs to be represented as a single object and will need to be thread safe. For this purpose you will need to synchronize on the file for short periods when doing a read or write. You will also need to provide the longer lived record locking that's specified in the requirements. The record locking is probably easier to implement if you use a HashMap rather than a Vector. The HashMap isn't synchronized, so you can control the synchronization yourself and the Map interface allows you to use the record number as a key.

I use a singleton for the database and multiple instances of the Data class to represent client access. There has been discussion of using a multiton for the database, but I believe this is unnecessarily complicated as there is only a single table required by this project.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: [B&S]: Using a Singleton class to represent the file resource