views:

165

answers:

3

I have a situation utilizing class table inheritance where the base object (which is abstract) is extended by specific types of the object.

For example, Person --> User --> Prospect

However, in some instances like with Prospect, sometimes it extends User and sometimes it doesn't. I can't reverse the relationship because User !== Prospect so I'm wondering how to best handle these kinds of scenarios where an object sometimes extends and sometimes doesn't.

Also, I know that someone will suggest composition over inheritance in this case, but that's truly not feasible because I'm relying on the ability to extend parent functionality and member properties are populated so that all properties are accessed in the exact same manner.

+3  A: 

I smell Decorator Pattern.

Peter Bailey
Great site linked. Bonus for being language specific.
Jomdom
+1  A: 

Inheritance in a database usually means the primary_key of the child table is a foreign_key to the parent table's primary_key.
But you mention the child could be an orphan. In this case the parent is an optional relation. (Usually solved by composition)

You could create two classes, one extending User and one that doesn't.

(But this may not be appropriate for what you are trying to do)

Bob Fanger
In this case, every child has a person_id inherited from the base table, but a prospect can inherit from either user or just from person.
Noah Goodrich
Ah, i see where you are going. If a Propect has a User it should get the extra properties and methods from User. Hmmmm, tricky...
Bob Fanger
A: 

Following the Decorator Pattern would be your best bet. Not sure what language you are using, but here's another link for a .NET implementation.

http://www.dofactory.com/Patterns/PatternDecorator.aspx

Justin Niessner