aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Is my Data class design Okay? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Is my Data class design Okay?" Watch "Is my Data class design Okay?" New topic
Author

Is my Data class design Okay?

Jianping Wang
Ranch Hand

Joined: May 29, 2010
Posts: 60
I have a Data class which uses DatabaseAccessor class. DatabaseAccessor has methods load(int recNo), persist(int recNo,String[] record), allocate(), size(), checkValidity(int recNo) and deallocate(int recNo) to directly access database file.
I have three questions:

Q1. I know Data class must be singleton, but not sure whether DatabaseAccessor class be singleton or not.
Q2. I also not sure create Data class firsrt or DatabaseAccessor first, the relationship between them should be composition or aggregation.
Q3. Is the methods in class DatabaseAccessor Okay? I use them because this class directly manipulate data file and contains data schema information.



SCJP 6 with 93%
Oracle Database SQL Expert with 98%
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2266
    
    3

Howdy, Jianping!

Q1. I know Data class must be singleton, but not sure whether DatabaseAccessor class be singleton or not.


Well champ, the Data class doesn't have to be a singleton. A lot of us implement it this way because it is easier to handle the locking mechanism. But it doesn't have to be a singleton.
Q2. I also not sure create Data class firsrt or DatabaseAccessor first, the relationship between them should be composition or aggregation.


Well champ, it can be either way. If you create the DatabaseAccessor object and pass it to the Data object, it will be aggregation. If you create the DatabaseAccessor inside the Data class, it will be composition.

Q3. Is the methods in class DatabaseAccessor Okay? I use them because this class directly manipulate data file and contains data schema information.


Well champ, as long as you obey the interface that was provided to you, it's ok. So I'd say yes, it is OK!


Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5407
    
  13

If you make sure only 1 instance of the Data class exists (either by applying the singleton design pattern or when you use the Data class, do something like private static Data data = new Data();), you will have the guarantee that only 1 instance of your DatabaseAccessor will exist too.


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Jianping Wang
Ranch Hand

Joined: May 29, 2010
Posts: 60
It's nice to have you two answer my question!!!

Here is another part of my design: I use a class

to handle data converting(from byte[] to String or vise versa) and validating (check range).
I am also want to introduce DataFormat class to populate those Fields when parse the database file. I am also think of keep data schema in one class...

So far I am afraid that my design is overcomplicated?
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2266
    
    3

Champion, do you mean java.lang.reflection.Field or did you create your own Field class?

Champ, I think your design is a little bit overcomplicated. You can use new String(byte []) and string.getBytes() to deal with the values. Although I think you are having a good attitude keeping everything separate, it's better to try to make things simpler.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5407
    
  13

I had also a seperate class to store the meta data (index, name and length) of each field. But I just stored it as a simple List and my file access and locking occured in the Data class. You are creating a whole lot of classes, maybe even too many.
Jianping Wang
Ranch Hand

Joined: May 29, 2010
Posts: 60
The class Field is my own product. Only contains name and lengthInBytes and method called convert to convert String to bytes.

Meta data leads to another issue: where to convert and validate data?

First checkpoint: in DataAccessor class.
Based on database file, we can only extract column name and its length in bytes and no data type information here. What we can do with those meta data is to make sure that the converted data not overflowed.


Second checkpoint: in ReservationManager class.
In this class, I convert String to DataType and do business there.

Keeping data checking in two different places seems not ideal to me. DataAccessor should have no idea of the meaning of data, all it can do is checking the length of each field. ReservationManager knows meaning of data, but it has to query meta data to know the maximum length of String.

What's your advice on that?
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5407
    
  13

In order to maximum reusability your Data class should only do database validations, no business validations. So checking if a field value isn't too long, is a check for the database. But checking if the customer id is an 8 digit number isn't.
 
Consider Paul's rocket mass heater.
 
subject: Is my Data class design Okay?