• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Enum factoring common functionalities.

 
Rajasekar Elango
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

In my application I have many enums having same functionalites/implementation..

For e.g I want toString() of all enums to return internalized string.

If inheritence is supported, I can abstract this to super class and have all enums extend.
But, Java 5 doesn't support inherting Enums.

Is there any good design approach for sharing functionalities across enums?

Regards,
Raja.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Huh, I can't think of a sample that would match what you are asking. Usually Enums are just used instead of other hacks for enumerated types, not really for creating "functions" It is true that you can override a method declared in an Enum, but to be used in rare occasions in my mind. Your example of toString is one that makes me think 1. You are using Enums in the wrong context. Or that 2. You might not understand that toString returns a viable use, but again, I would say if you are using toString for logic, then Enums are probably being used in the wrong context or not using them correctly.

Please post an example of what you want to accomplish.

Thanks

Mark
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mark Spritzler:
It is true that you can override a method declared in an Enum, but to be used in rare occasions in my mind.


Mhh, I'm doing this all the time. It's an elegant way to implement the Strategy/State patterns, in my opinion.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I mentioned the strategy technique in this thread: http://www.coderanch.com/t/378657/java/java/Switch-abuse
I quoted the "Operation Enum + eval method" example from the JLS and Tony Morris wrote "ick" and "Binding an enum to an implementation detail (which is completely unrelated to an enum) is horrible best case scenario." I felt so dirty, and it wasn't even my code!
 
Rick O'Shay
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Enumerated types already have methods to go from string to enum and back again. That lets you use them outside (e.g. an HTML page or XML document) while converting them back to typesafe enums when coming back in to Java.

See: name(), valueOf() and ordinal()

Anything more and you are abusing the purpose of an enumerated constant in my opinion.
 
Rajasekar Elango
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the Example code:


DisplayFormat is another enum with various formats.
I dont want to override toString() method with display format parameter,
because i am passing Planet.values() to combo box constructor and it will
use only default toString() method.

Assume that Internalization.getString(key) will return localised string.

I have many enums having same implementation for setDisplayFormat() and toString().

Am I misusing enums..? All your valuable suggestions are welcome..

Thanks,
Raja
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure whether I'd call it a "misuse" - to me it would mostly depend on how the enum gets used in the rest of your code.

Regarding your toString implementation, I'd highly recommend using a custom ListCellRenderer in your combobox. That way you move the logic in your toString method into its own, reusable class and decouple the business logic from presentation logic.
 
Rajasekar Elango
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ilja

Regarding your toString implementation, I'd highly recommend using a custom ListCellRenderer in your combobox. That way you move the logic in your toString method into its own, reusable class and decouple the business logic from presentation logic.


I will be using Planet enum with other swing components like JTable.
Putting it in toString() make it re-usable everywhere..

Thanks,
Raja
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic