Re: Retrieving unnecessary data



ShaneLM <shane.niebergall@xxxxxxxx> 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.

That's a real problem all right. What object/class in the system is so
interested in Car's internals? It sounds like you have a very non-OO
system already.

A) Return the class with all fields filled out, regardless of whether
they will be used or not. This is OO, right?

No. OO is about behavior not fields. What does a Car *do*?

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).

None of these are not OO. Users of Car should not be obsessing about the
state of a particular Car object's fields. They're private remember?

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?

In OO, objects do things. "I want access to the data that is in object
X." is a clear indicator that you are not writing OO code.
.



Relevant Pages

  • Re: Whats going on here?
    ... "A BUSINESSWOMAN who was over the drink drive limit when she ran down ... "They were killed instantly but Butres and her passenger John ... Note that the passenger was, in fact, the owner of the car and was ...
    (uk.legal)
  • Re: OT: (mostly) Busted grille
    ... I couldn't believe THAT car didn't roll. ... the tire industry should probably be required to raise their ... unbelted passenger in the backseat, hit a streetlight at the TOP of the ...
    (rec.outdoors.rv-travel)
  • Re: Road wars
    ... with the police and films them. ... I saw in one clip an unmarkedpolice car was following a car that ... whilst the other goes over to the passenger side, ... driver who is sitting with his feet up inside the car having a chat ...
    (uk.legal)
  • Re: Update to the saga
    ... I am going to try and get the passenger side of the top-end ... reinstalled tomorrow and then tear the drivers side off. ... 'less important' stuff was thrown away before I got the car. ... originals, cracked, and basically leaked all the hot air out so the ...
    (rec.autos.makers.vw.aircooled)
  • Re: Road wars
    ... I saw in one clip an unmarkedpolice car was following a car that had ... pulled into a car park of its own accord and the driver and passenger got ...
    (uk.legal)