File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Domain class design question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Domain class design question" Watch "Domain class design question" New topic
Author

Domain class design question

Warren Bell
Ranch Hand

Joined: Dec 20, 2000
Posts: 56
I have a class called Order and a class called LineItem.





Now this is fine if I am retrieving from the db the entire order with all the lineItems. But most of the time I am retrieving just one lineItem. I still need the order info too. I have thought of doing this:



This seems sloppy using one member currentLineItem and a List lineItems together. I have also thought of doing this to retrieve the current lineItem from order:

LineItem lineItem = order.getLineItems().get(0);

This seems sloppy too, since I can never really be sure that the List will only have one LineItem in it without checking and it is the one I retrieved. I like the first example better, but it still doesn't seem right. What would be the best way of representing this Order class given that most of the time I am only needing to retrieve one Order with one lineItem?

Thanks,

Warren
[edit]Please use the code button; I have edited this post so you can see how much better it is to read. CR[/edit]
Warren Bell
Ranch Hand

Joined: Dec 20, 2000
Posts: 56
I have got this post in the wrong forum. It should be in Intermediate.

Warren
Martijn Verburg
author
Bartender

Joined: Jun 24, 2003
Posts: 3274
    
    5

Hi there,

You simply need to check your list of line items first:

Is it empty? If it's not empty then grab the 1st line item as the current line item (Note, you're assuming that the first item in the list is the current line item, but are you guaranteeing this definition/order?) as you stated above ...get.(0)..

Cheers, Martijn - Blog,
Twitter, PCGen, Ikasan, My The Well-Grounded Java Developer book!,
My start-up.
Warren Bell
Ranch Hand

Joined: Dec 20, 2000
Posts: 56
Is this considered good design? It just seems like there is too many checks that would have to be made.

isEmpty() == false

size() ==1

plus I would have to make sure the List was empty every time I retrieved a new LineItem.
Martijn Verburg
author
Bartender

Joined: Jun 24, 2003
Posts: 3274
    
    5

The two checks would not be considered bad design though. But do think about how you can have the actual code for that only in one place.

Why would you need to check if the list was empty every time you get a line item?
Rosco Duncan
Ranch Hand

Joined: Apr 23, 2007
Posts: 41
Have you considered adding an Order property to the LineItem?

This would be the inverse relationship to the List of LineItems that the Order has. The idea being that if you were to get a handle to an individual LineItem, you would then have the reference back to its Order.

Are you doing some sort of Object Relational mapping to load these from the database?
 
Don't get me started about those stupid light bulbs.
 
subject: Domain class design question