• Post Reply Bookmark Topic Watch Topic
  • New Topic

design problem - beginner  RSS feed

 
Debasish Mukherjee
Greenhorn
Posts: 2
Eclipse IDE Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm new to designing java-application components and I should confess that I have not done much study on this topic.
I have this scenario - Class Animal is Super Class. Camel, Dog etc. extends animal with specific behavior. But while saving into DB we are maintaining only one db-table : 'animal_detal'. This table stores basic info of all animals.
Here is my approach -
Parent Class - Animal (Abstract Class) :
String name, String animalType, <more member instance variables common to all animals>....
Method : getInstance() - returns Instance of Animal @ runtime (i.e. returns reference to Camel, Dog), <more methods/ behaviors common to all animals>

Sample Child Class - Camel Extends Animal :
<only methods/ behaviors specific to Camel – no new instance variables>
<public static Camel createFromResultSet(ResultSet rs) - creates and returns a Camel object from ResultSet>

The problem starts when I try to retrieve from ‘animal_detal’ – based on my current structure how can I decide which object to create for a db-row. Please help/suggest.
 
Campbell Ritchie
Marshal
Posts: 56522
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is a peculiar design. It is usual to store data in a database, not whole objects. It would be more usual to
  • use some sort of bean pattern to store the state of the objects in an XML file
  • or to serialise the objects, maybe in a List
  • It is however possible to store the class names in the database, and use Class.forName(...).newInstance() or similar to create those objects. Note there are special requirements and problems about that technique. You need a no-arguments constructor and it declares checked Exceptions.
     
    Winston Gutkowski
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Debasish Mukherjee wrote:...But while saving into DB we are maintaining only one db-table : 'animal_detal'. This table stores basic info of all animals...

    Which is probably a poor design unless, as Campbell suggests, the table actually contains serialized objects - in itself not the best use of a database (why wouldn't you just put them in a file?).

    Individual animals are likely to have specific attributes that distinguish them (eg, Camel: number of humps), which to my mind suggests a table hierarchy similar to your class one, or at the very least one that involves storing extra attributes.

    However, in answer to your question, I reckon you'll need some sort of factory pattern: The simplest would probably be a dispatch method in your Animal class that uses animalType to determine which object to create (an enum allied with a Map could be used to make the code less procedural). An alternative would be to have each animal class "know" how to create itself from database data - indeed the two possibilities could be used together.

    HIH.

    Winston
     
    Campbell Ritchie
    Marshal
    Posts: 56522
    172
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Winston Gutkowski wrote:. . . An alternative would be to have each animal class "know" how to create itself from database data - indeed the two possibilities could be used together.

    HIH.

    Winston
    What a nice idea. You can pass the ResultSet as a constructor parameter. You would have a lot of scope for IllegalArgumentExceptions, however!
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!