Re: Retrieving unnecessary data



ShaneLM wrote:
I'd like some advice on how to structure my classes. Let me use an
example to convey my situation. Pretend I am modeling a Car.

Class Car
{
String make;
String model;
Enum color;
List<People> passengers;
}

My problem arises because sometimes I'm just interested in the stats
of the car, and not the people in the car. Although in other
situations I do care about the people in the car.

If I'm writing a database access method, say GetCar(), I feel like
time is wasted retrieving info about the passengers that I will never
use in some situations. So I feel like I have a few alternatives:

A) Return the class with all fields filled out, regardless of whether
they will be used or not. This is OO, right?
B) Create overloads for GetCar where I indicate whether I want all
fields filled out for me. Ex: GetCar(bool fill_passengers);
C) Remove the passengers field, and retrieve them with another call.
I'd call GetCar(), and then if I want the passenger list, I'd call
GetPassengers(Car);
D) Make 2 classes - Car and CarWithPassengers (that can derive from
Car).

Does that make sense? I know it doesn't sound like retrieving a
passenger list is expensive, but I'm trying to keep this example
simple. Consider if the People class had lots of nested classes inside
of it, and all we wanted to know was the person's name? Same situation
occurs...

Which alternatives do you prefer? Is there anything else you would
suggest?

Thanks so much!!
Shane
Hibernate (a particular Java ORM technology) allows you to specify either lazy or eager loading. It also allows you to override this at query time.

I suggest that initially you should ignore the extra burden of eager loading (solution A). If you find in practice that this causes performance problems, then try solution B.

--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
.



Relevant Pages

  • Re: Retrieving unnecessary data
    ... Pretend I am modeling a Car. ... Typically that would be done with a collection class and would have a reference to the collection class. ... If no one but ever needs to know about passengers, then that reference could be private or the collection class instance could be an embedded object in 's implementation. ... If the solution needs both then an interface method would provide that. ...
    (comp.object)
  • Re: Mass Transit
    ... What about passengers, then (since I am almost always a passenger ... a car, bus, train, plane, etc., is very different from driving one. ... Yet a passenger in a car can do useful things to help the driver, ... show how you will route transit to where presently non served by any ...
    (rec.arts.sf.fandom)
  • Re: basic q about sequence diagrams
    ... and the Client needs to collaborate with the Car's Passengers. ... Client sends a message directly to each Passenger. ... "Collection* R2" in [Car] that only a code generator could love. ... That seems like a reasonable responsibility of. ...
    (comp.object)
  • Re: Minibus hire company backs out of arrangement.
    ... I didn't think I was proposing to use the vehicle for hire or reward. ... people who use their car to take ... Your policy covers you for carrying passengers for social or similar ...
    (uk.legal)
  • Re: Aylesbury Vale Parkway
    ... steelwork for the station building is erected though and it looks like ... north of Aylesbury and indeed the northern suburbs of the town itself. ... Free car parking for the first year should act as a suitable carrot. ... passengers on train with me towards London. ...
    (uk.railway)