Search code examples
javajavabeansvalue-objectsenum-map

Might EnumMap be considered a reasonable alternative to Java beans?


Curious if anybody has considered using EnumMap in place of Java beans, particularly "value objects" (with no behavior)? To me it seems that one advantage would be that the name of a "property" would be directly accessible from the backing Enum, with no need for reflection, and therefore I'd assume it would be faster.


Solution

  • It may be a little faster then using reflection (I didn't measure it, didn't find any metrics in Google either); however there are big disadvantages to this approach:

    1. You're losing type safety. Instead of int getAge() and String getName() everything is Object get(MyEnum.FIELD_NAME). That'll provide for some ugly code and run-time errors right there.

    2. All the javabean niceties we've come to love and enjoy (for example, property-level annotations) are gone.

    3. Since you can have NO BEHAVIOR AT ALL, the applicability of this approach seems rather limited.

    The bottom line is - if you really truly need that alleged :-) boost in performance (which you'll have to measure to prove it exists) this may be a viable approach under very specific circumstances. Is it a viable alternative to javabeans at large? Most certainly not.