My title is a bit vauge, here's the problem I'm talking about.
Say I have:
class Bill
{
public String legislationID;
public BillStatus currentStatus;
}
//and
class BillStatus
{
public String description;
}
When persisted, BillStatus corresponds to a table with one row per possible state of a piece of legislation. (e.g. "On House Floor", "On Senate Floor", etc).
The problem I always run into is this:
As soon as behavior starts to vary based on which instance of BillStatus is attached to Bill, the whole thing becomes hard to represent. I see two options:
1) A lot of switches in Bill that branch behavior depending on current BillStatus 2) methods in bill status that accept Bill as a parameter, and have switches that execute different logic depending on identity of instance. (so kind of mimicking classes implementing a common interface)
Both these methods seem sloppy. What I would like to be able to do is this:
class abstract BillStatus
{
public a开发者_高级运维bstract String getDescription();
public abstract void callForVote(Bill bill);
}
Then:
@SpecialEntityTag(whenRowValue="HOUSEFLOOR")
class BillStatusHouseFloor extends BillStatus
{
/* special overrides for some methods */
}
class BillStatusGeneric extends BillStatus
{
/* standard implementation of BillStatus */
}
This has to be a common problem. Am I thinking about it the wrong way? Basically the goal is to encapsulate behavior that changes with particular value of a non-primitive child property.
What prevents you from using your design?
Hibernate (or JPA in general) is able to handle inheritance, and you may store a whole inheritance hierarchy in a single table with a discriminator column telling which subclass to use for a particular row.
See http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html_single/#inheritance-tableperclass for details.
精彩评论