HELP! Design ideas for an inheritance problem.

  • Thread starter Thread starter Darren
  • Start date Start date
D

Darren

I have a Person object and from that I'm inheriting other objects
including Client, Carer, Doctor (and others). The Person object has a
large number of properties and methods that I want available in
Client, Carer and Doctor.

Person <- Client
Person <- Carer
Person <- Doctor

My problem however is an individual can be a Client and a Carer so in
this situation I want multiple inheritance.

Person <- Client
<- Carer

Another time an individual might be a Carer and a Doctor.

Person <- Carer
<- Doctor

Obviously I can't do this. I need some design that allows me to test
whether a Person is a Carer, Client or Docotor and then get the object
for it.

Any ideas?

Thanks,
Darren.
 
Hello!

Darren said:
I have a Person object and from that I'm inheriting other objects
including Client, Carer, Doctor (and others). The Person object has a
large number of properties and methods that I want available in
Client, Carer and Doctor.

Person <- Client
Person <- Carer
Person <- Doctor

My problem however is an individual can be a Client and a Carer so in
this situation I want multiple inheritance.

Person <- Client
<- Carer

Another time an individual might be a Carer and a Doctor.

Person <- Carer
<- Doctor

Obviously I can't do this. I need some design that allows me to test
whether a Person is a Carer, Client or Docotor and then get the object
for it.

Any ideas?

I would define interfaces for non-instrinsic features.
Instread of defining classes like Doctor and Carer, you may define
interfaces like IDoctor and ICarer (or with better names).
Now you inherit from Person and implement both IDoctor and ICarer.
At least, this is how you mimic multiple inheritance.


Sam
 
No, you DON'T want multiple inheritance. If there are naming collisions or -
worse - redundant implementations (you're gonna get update anomalies, just
because it's in memory you aren't off the hook for normalising your data
model) then how are you going to determine precedence?

Don't inherit. Aggregate.

Give Person a collection of Roles. That way you can add Role instances till
the cows come home and access all of them with no semantic ambiguity. If
there is a many to many relation between Person and Role then do the same as
you would in an on-disk database and relate them with another table
PersonRole. You're gonna need either doubly-linked-lists or two hashtables
for each relation (so you can traverse both ways).
 
Luckily I readed your text before I started typing.

Just to tell that I agree with you.

Cor
 
Back
Top