I have a simple question, which has hopefully a simple answer. I'm sure this has been hashed before, but I'd like to get the opinion of people in this forum...
I have certain abstractions in my system, which may or may not be persisted. If the entity is persisted, it may be referenced/"looked up" by a primary key. (Obvious scenario is database).
So, I'm pretty confident that I don't want the "core" entity object to contain a notion of a "primary key" since it doesn't make sense if the object is not persisted. The question is, what are the tradeoffs in using inheritance vs containment for the persisted entity?
Here's a simple example of the two paradigms:
// Containment
interface Entity {
public Object getData();
}
interface PersistedEntity {
public PersistedEntityPK getPK();
// must call getEntity() method to call any of Entity's members...
public Entity getEntity();
}
// Inheritance
interface Entity {
public Object getData();
}
interface PersistedEntity extends Entity {
public PersistedEntityPK getPK();
// all other Entity methods inherited from super interface
}
It seems to me like inheritance might be the way to go, if nothing else for the substitution argument. Any thoughts for/against?