aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Data constructor arguments & database cache Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Data constructor arguments & database cache" Watch "Data constructor arguments & database cache" New topic
Author

Data constructor arguments & database cache

C. Koch
Greenhorn

Joined: Feb 24, 2010
Posts: 3
Hi,

for my assignment (URLy Bird) I am using a rather generic database class. My assignment only states:

"Your data access class must be called "Data.java", must be in a package called "suncertify.db", and must implement the following interface:[....]"

To avoid using a singleton I use a factory method with the signature

where Data is package-private (originally it was a private static inner class, but that way I'd have a file Data.java containing no instantiable class called Data, which I guess would only be legal if this was a lawyer exam, and not a programming one....(actually you can't name your class Data.java even if you want to, but thats another story))
The assignment does not say anything about providing a default constructor, so is it possible to get away without one, or do I have to provide one, thus ruining my generic approach? My second question is about the size of the database. I use a cache, which means my Data object grows in size linear to the data file, inevitably resulting in an out of heap space error for very big files. anyone else has done this, or do I need to check the file size and enable / disable the cache accordingly?

Thanks in advance
Christian
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 4908
    
  10

Hi Christian,

First of all a warm welcome to the JavaRanch!

The rule is easy: a "must" requirement you must do exactly what it says (or you risk automatic failure), otherwise you decide what to do (and you can explain your decisions in choices.txt). So you don't have to provide a default constructor.

Data.java should look like this (according to the must requirement):
Your class could be either public or package private (my own Data class is package private).

I don't understand why you have to pass a magicCookie and a set of strings to your getInstance method in your Factory.

Please could you edit your 1st post and always format your code (max 80 chars per line) when using code-tags. So the thread can render without using horizontal scrollbars.

Kind regards,
Roel


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
C. Koch
Greenhorn

Joined: Feb 24, 2010
Posts: 3
Hi Roel,
I don't understand why you have to pass a magicCookie and a set of strings to your getInstance method in your Factory.


My approach is quite generic. You can pass any primary key (Set<String>) and magic cookie to it, and it configures itself (using the file header) according to your constructor arguments. So my idea was to read the configuration in my main() method and than construct a Database according to it. If I read the .properties file while constructing the database I get a somewhat tight coupling between my database and my suncertify.properties. I am only allowed to use a single .properies file, and from an architectural point of view I do not feel that coupling a database with a file containing gui and network configuration is clean design.

cheers
Christian
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 4908
    
  10

Hi Christian,

Nobody requires you to read properties file when constructing the database.

In my solution the suncertify.properties is only accessed from my gui-package and I use the path to the database file to initialize my Data class. Other ranchers have 2 static getInstance-methods: one without a parameter and one taking the path to the database file as parameter. And there are many other alternatives I guess.

So assume your database file has room numbers as primary keys. Your method can define other primary keys for each record Why would you need something like that?

Maybe I'm missing something here, but it seems to me you are overcomplicating your solution.

Kind regards,
Roel
C. Koch
Greenhorn

Joined: Feb 24, 2010
Posts: 3
Roel De Nijs wrote:
In my solution the suncertify.properties is only accessed from my gui-package and I use the path to the database file to initialize my Data class.


So what do you do about your magic cookie? It sounds like your Data class is specifically designed to serve hotel room records from a file with a (hardcoded?)magic cookie xyz and a primary key "roomnumber", so it's reusability is limited but it needs very few configuration. My Data class is able to read any file following the given data file specification and doesn't care if said file contains records consisting of 6 fields with no primary key or 9 fields with a primary key combined out of three fields. To do that it has to get a valid cookie for the used schema to compare with the given file and information which fields make up the key. So my class is very flexible but requires more configuration to achive that goal.

cheers
Christian
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 4908
    
  10

Hi Christian,

My Data class reads the database dynamically. So it can update, delete, read,... any file with the same structure as this database file. The only problem would be the magic cookie. For the assignment it is hard-coded, but when there were no requirements concerning the Data class, I would have done the following:
  • make Data abstract class and have 1 abstract method (checkMagicCookie)
  • create a concrete RoomData class which extends the Data class and the singleton design pattern is applied to this class (because I prefer the simplest approach possible for making it thread-safe): singleton + mark all public methods synchronized)

  • When I need a Data class for a database file containing customers, I just need to create CustomerData which extends Data, implement the checkMagicCookie and apply a singleton design pattern. And that's it! So my Data class is 100% resuable
    I also added a dao factory pattern (that's the only feature I added to the program which was not asked/required, but I thought it was useful to improve reusability, extensibility, flexibility,...) which makes it very easy to work with different database types (flat file for customers, rdbms for rooms,...).

    I added both explanations to my choices.txt and I even created a thread about making Data class abstract in this forum (when I was in final stage of writing my decisions document).

    Kind regards,
    Roel
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Data constructor arguments & database cache
     
    Similar Threads
    (B&S) The read() method
    NX: Adapter class
    URLyBird 1.2.1 Passed!
    Application Design
    URLyBird - 'Your data access class must be called "Data.java"'