Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

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

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 1033
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic