Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is my Data class design Okay?

 
Jianping Wang
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Roberto Perillo
Bartender
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Roel De Nijs
Sheriff
Posts: 10386
132
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jianping Wang
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 10386
132
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 10386
132
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic