Re: Retrieving unnecessary data
- From: topmind <topmind@xxxxxxxxxxxxxxxx>
- Date: Fri, 15 Feb 2008 15:40:57 -0800 (PST)
On Feb 15, 7:35 am, Robert Martin <uncle...@xxxxxxxxxxxxxxxx> wrote:
On 2008-02-14 13:26:27 -0600, ShaneLM <shane.nieberg...@xxxxxxxx> said:
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;
}
First let me point out that you are not modeling a class. You are
modeling a database table, or a data structure. Classes expose
behavior and hide data.
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.
Your class should be defined as follows:
public class Car {
public CarStats getStats() {...}
public List<People> getPassengers() {...}
}
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.
You can implement getStats() to just fetch the stats. You can
implement getPassengers() to just fetch the passengers.
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?
No, this is database thinking, not OO. OO does not map directly to
databases. OO is orthogonal to databases. OO is about behavior, not
data.
B) Create overloads for GetCar where I indicate whether I want all
fields filled out for me. Ex: GetCar(bool fill_passengers);
We found everyone who did things like that and shot them.
C) Remove the passengers field, and retrieve them with another call.
That's closer. You should *hide* all the fields, and retrieve them
whenever you need them.
I'd call GetCar(), and then if I want the passenger list, I'd call
GetPassengers(Car);
Or rather, car.GetPassengers();
D) Make 2 classes - Car and CarWithPassengers (that can derive from
Car).
We shot those people too.
If all the OO proponents who have differing views of what OO *should*
be shot each other, there'd be none left. (Can't say I'd miss 'em)
--
Robert C. Martin (Uncle Bob) | email: uncle...@xxxxxxxxxxxxxxxx
Object Mentor Inc. | blog: www.butunclebob.com
The Agile Transition Experts | web: www.objectmentor.com
800-338-6716 |
-T-
.
- Follow-Ups:
- Re: Retrieving unnecessary data
- From: Robert Martin
- Re: Retrieving unnecessary data
- References:
- Retrieving unnecessary data
- From: ShaneLM
- Re: Retrieving unnecessary data
- From: Robert Martin
- Retrieving unnecessary data
- Prev by Date: OO Methodolgy Perspective (was Re: OOSE is outdated ?)
- Next by Date: Re: OO Methodolgy Perspective (was Re: OOSE is outdated ?)
- Previous by thread: Re: Retrieving unnecessary data
- Next by thread: Re: Retrieving unnecessary data
- Index(es):
Relevant Pages
|