Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Similar Classes......Or are they the same? 2

Status
Not open for further replies.

Craig0201

Technical User
Oct 11, 2000
1,261
GB
Hi,

Best to explain this by analogy.....

Imagine a 100 Metre track and field race.

A race contains a number of runners each of which produces a performance.

A runner has a number a performances over a number of races.

Races and runners therefore both contain the collection object Performances. But for each performance within a race context, I need to know the runner ID, not the race ID (contained in Race) and for each performance in a runner object I need to know the race ID, not the runner ID (contained in race)

Now I know that I could create a generic Performance class and inherit it and add the property I need in a further 2 classes. But this seems intuitively wrong.

I'm sure there is a better way of doing this. Can anyone help, please?

Craig
 
In data modelling terms, Performance would be an entity that represents the linkage between runners and races (a 'many-to-many' relationship). Once implemented as a table, this gives you a single source for "all races for a runner" and "all runners for a race".

Taking a simple approach, why not just implement that relationship as a class, so you have one class with runner and race ids. There's probably a pattern for this, but I can't think of it (too early!)

HTH,

Mike
 
Mike,

Thought of that. That doesn't seem right to me either. DBwise, agreed but it doesn't seem to fit with OO.

But much appreciated comment!

Craig
 
Mike,

Mention of patterns had me rooting around. Do you think this fits with Mediator?

Craig
 
So you'd have a class that delivered, say

RunnersFor(Race) and Racesfor(Runner)

methods?
 
As I thought....my thought was gibberish!

How about describing an interface with the two methods and having the classes implement that interface and inherit from the superclass?

Craig
 
I agree with mikewoodhouse. In fact, to identify a performance you need both the runner and the race.

If you are using persistent objects, I can imagine that you have a
Code:
Performances
collection that handles the persistence.
Your
Code:
Runner
object would then have a
Code:
performanceForRace
method (delegating to the
Code:
Performances
collection, passing itself as the runner) and a
Code:
Race
object would have a
Code:
performanceForRunner
method.
If there is not a performance for each race and/or runner, both the
Code:
Runner
and the
Code:
Race
would have a
Code:
performances
collection with the
Code:
performances
that do exist.

Hope this helps or amuses
 
Don/Mike,

Convinced.

Thanks for the help.

Craig
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top