• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Data class in URLyBird

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am working on URLyBird for my SCJD exam. And I have on question on modelling the records saved in database.

I have read the schema from provided database file, and it matches the schema provided in the instruction and I am able to read all the data. But comes to modelling of the records saved in the database I have two options, which I am not sure which one to choose.

1. Model it as a POJO
This is most straighforward way, I can create a Record class with all necessary fields, e.g. name, location, size... and pass all the necessary values in to the object, persist in the file.

But doing so is obviously rigid, and defeats the purpose having the schema up front.

2. Model it as a list of FieldValue
Another option I have is to create a class of FieldValue, which holds the flag, name and value of the field. And each record will hold a list of FieldValues.

This approach is more flexible, and honor the schema declared. However, the requirement reads:

A clear design, such as will be readily understood by junior programmers, will be preferred to a complex one, even if the complex one is a little more efficient. Code complexity, including nesting depth, argument passing, and the number of classes and interfaces, should be reasonable.



Actually I am 80% percent sure that approach 1 should be the way to go, but I wanna play safe and seek further confirmation from the forum.

Thanks a lot.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Wang,

Welcome to the JavaRanch!

I'll describe my approach: in my Data class I used String[], because all method signatures use String[]. The method signatures of my business service use a Room POJO. So in my business service the String[] is converted to the POJO (and vice versa). In the GUI of my client I also work with the room POJO. Advantages of using a POJO in the GUI are described here.

In your 2nd alternative you mention

which holds the flag, name and value of the field

Do you mean the "deleted" flag (which indicates if a record is valid or deleted)? I think this flag has nothing to do with a field value, so 2nd alternative is no good from a design perspective.

Kind regards,
Roel
 
Wang Yizhuo
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply.

I made a mistake for my approach two, the valid/deleted flag should belong to Room and the FieldValue will only consists of two fields, name and value.
 
Bartender
Posts: 2292
3
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy, Wang!

I made a mistake for my approach two, the valid/deleted flag should belong to Room and the FieldValue will only consists of two fields, name and value.



Hum... here's what I did: I created a class called Room, which has all fieds that the record has, except for the record number field, with the proper types for each field. It's just that this approach is more object-oriented than having, for instance, a class called Record.
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Wang,

In my opinion the "deleted" flag doesn't belong to the object Room (Record) nor the String[]. If the flag indicates that a record is "deleted", this record simply does not exist. So how can you have an object (array) of a non-existing record.

Kind regards,
Roel
 
Wang Yizhuo
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply

To sum up, it should be OK with my approach 1, which uses POJO.

For delete flag, my current intention is to load everything from file to memory and process from there. So when I do searching I will just search the Rooms in memory whose flag is VALID instead of DELETED. Of course I can skip loading the object into the memory altogether, so I dun even need the flag in my POJO.
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or instead of storing a POJO for a deleted record, just store null (that's what I did btw )
 
Roberto Perillo
Bartender
Posts: 2292
3
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, forgot to say that I also didn't include the deleted flag in my Room object.

Roel De Nijs wrote:Or instead of storing a POJO for a deleted record, just store null (that's what I did btw )



Me too!
 
Bras cause cancer. And tiny ads:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic