On Wed, Nov 11, 2009 at 11:40 AM, Ben R Vesco <[email protected]> wrote:
> >> Regarding the enums, we did consider them in several places but they
> prevent
> >> addition of new constants by the framework or by the developer,
> especially
> >> without breaking existing code.
>
Framework users wouldn't be able to add RouterMatchMode values (but see
below), but framework developers most certainly could. The enum facility was
designed so that changes to an enum type would not break binary
compatibility. One of our key design goals in JSR 201 was to ensure that it
would be possible to add values to an enum without requiring recompilation
of existing clients of the enum type.
> They also produce extra artifacts in the Javadocs cluttering them.
>
Compare the int constants in Router to any of the nested enums in the
standard library (e.g., java.Text.Normalizer.Form), and I think it's clear
that putting the values in a separate artifact is a *good* thing. It makes
the enclosing type easier to read, because you don't have to wade through a
lot of constants before getting to the point of the class.
Effective Java, 2nd edition, Item 30 has a passionate endorsement of enums
over int constants.
> Regarding enums and extensibility, this pattern works quite well:
>
> interface MatchMode;
>
> enum RouterMatchMode implements MatchMode {
> BEST,
> CUSTOM,
> FIRST,
> LAST,
> NEXT,
> RANDOM,
> ;
> }
>
> void Router::setDefaultMatchingMode(MatchMode mode);
>
> Then a developer wanting to extend the match mode need only implement
> the marker interface MatchMode on their new enum and they are
> interchangable with those provided by the library. This implementation
> style also makes it easier to extend the match modes because you don't
> have to sit around thinking and double checking, "now should my new
> mode be index 5 or is that used by one in the lib already?"
>
For more about this approach, see EJ2ed, Item 34: Emulate extensible enums
with interfaces.
This could be a huge win for Restlet.
--tim
------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2417095