It's not an unknown problem you describe, people modeling with JPA and
Enum's experience similar things. Enum's are really only beneficial
with truly static constructs, since an Enum instance on the JVM is the
perfect singleton which you can not instantiate yourself! While you
can hardwire logic to go fetch a description from some resource file,
it would be nasty putting any kind of database access down here.

An controversial solution might be to dispatch from the Enum to
elsewhere:

interface SuitDescriptor {
    String getDescription(Suit suit);
};

enum Suit {

    Diamonds, Hearts, Clubs, Spades;
    private static SuitDescriptor descriptor;

    public static void setSuitDescriptor(SuitDescriptor descriptor) {
        Suit.descriptor = descriptor;
    }

    public String getDescription() {
        if(descriptor == null)
            throw new IllegalStateException("Description callback not
specified!");

        return descriptor.getDescription(this);
    }
}

It's really a case where extension methods would come in handy, but
Java doesn't have that yet.

/Casper


On Mar 23, 9:30 pm, Mike Calmus <[email protected]> wrote:
> I am in the process of converting several custom enumeration classes to the
> Java 5 enum framework. This is going generally smoothly since our pattern
> pretty much conformed to the generally-accepted style. There are a few of
> these classes, though I'm struggling with. For some of our enumerated types
> we retrieved values from the database to populate descriptions. This isn't a
> problem for the named enums from this part since I can do the same lookup in
> a constructor. Part of the point of doing these database lookups originally,
> though was to have new values included in the enumeration without requiring
> a coding change to explicitly name new values. These implicit (from the
> database) enum values are the problematic pieces. Anyone have any ideas how
> to deal with this?
>
> Thanks.
>
> --
> Mike Calmus

-- 
You received this message because you are subscribed to the Google Groups "The 
Java Posse" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/javaposse?hl=en.

Reply via email to