aspose file tools*
The moose likes Java in General and the fly likes Java Enum factoring common functionalities. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java Enum factoring common functionalities." Watch "Java Enum factoring common functionalities." New topic
Author

Java Enum factoring common functionalities.

Rajasekar Elango
Ranch Hand

Joined: Sep 13, 2004
Posts: 105
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.


SCJP 1.4
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
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!


There is no emoticon for what I am feeling!
Rick O'Shay
Ranch Hand

Joined: Sep 19, 2004
Posts: 531
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

Joined: Sep 13, 2004
Posts: 105
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

Joined: Jul 11, 2001
Posts: 14112
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

Joined: Sep 13, 2004
Posts: 105
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java Enum factoring common functionalities.
 
Similar Threads
Enums
grammar help: what is this structure?
What is Enums?
Problem with ArrayList toArray method
alternative for interface