| 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.
|
 |
 |
|
|
subject: [B&S]: Using a Singleton class to represent the file resource
|
|
|