Talk:Enum
From APIDesign
(Comment provided by Tim Boudreau - via ArticleComments extension) |
(Comment provided by Ademir - via ArticleComments extension) |
||
Line 56: | Line 56: | ||
--Tim Boudreau 18:20, 23 September 2013 (CEST) | --Tim Boudreau 18:20, 23 September 2013 (CEST) | ||
+ | </div> | ||
+ | == Ademir said ... == | ||
+ | |||
+ | <div class='commentBlock'> | ||
+ | As enums can provide also mehtdos, I think it's better this way:public enum Colors { RED( #ff0000 ), BLUE( #00ff00 ), GREEN( #0000ff ); private String css; private Colors(String css) { this.css = css; } public String toCSS() { return css; }} | ||
+ | |||
+ | --Ademir 07:35, 21 October 2013 (CEST) | ||
</div> | </div> |
Revision as of 05:35, 21 October 2013
Comments on Enum <comments />
Contents |
stephane.appercel@neuf.fr said ...
Axel said ...
We ran into this same issue last year and had to replace enums by classes in some places.
However, sometimes enums make life easier. An API can be extended to support enums letting the user the freedom to use them or not. As an example here is a method I use for creating swing comboboxes. The standard version can be used with any class, but the enum version adds value in automatically determining the possible item values:
public <E extends Enum<E>> JComboBox addComboBox( String label, final Accessor<E> accessor, String constraints) { E[] values = DataUtil.enumValues(accessor.getType()); return addComboBox(label, accessor, values, constraints); } public <E> JComboBox addComboBox( String label, final Accessor<E> accessor, E[] values, String constraints) { ... }
--Axel 10:44, 15 January 2011 (CET)
Looks like your API also benefits from the Enum marker superclass. Otherwise you could not type the first method... --JaroslavTulach 22:14, 16 January 2011 (UTC)
Tim Boudreau said ...
A way around this is to implement an interface on your enums, and code to the interface wherever possible.
If you have code which *really* needs enums, you can use the signature <T extends Foo & Enum<T>> to require one, without specifying the exact type.
--Tim Boudreau 18:20, 23 September 2013 (CEST)
Ademir said ...
As enums can provide also mehtdos, I think it's better this way:public enum Colors { RED( #ff0000 ), BLUE( #00ff00 ), GREEN( #0000ff ); private String css; private Colors(String css) { this.css = css; } public String toCSS() { return css; }}
--Ademir 07:35, 21 October 2013 (CEST)
As you mentioned it, the decision to define a finite set of module categories was not correct, as it would have been possible to foresee the needs for custom module categories.
However, there is a one simple solution for this kind of problems. First, define a ModuleCategory interface, with some operations to get the properties of a module category. Second, define an enum called StandardModuleCategory, which implements this interface and define the standard module categories. Finally, define a class called CustomModuleCategory that also implements this interface. And use the interface everywhere in your code. You can also create an utility class called ModuleCategoryProvider with a single method named lookupByName(), or have a complete ModuleCategoryRepository.
--stephane.appercel@neuf.fr 16:05, 13 January 2011 (CET)
Simple solution except hidden API catches! Remember the enum already exists in your API, you cannot define it as StandardModuleCategory! You can't just: use the interface everywhere in your code! Your whole API is already using the old enum, you need to keep it unchanged in order to preserver BackwardCompatibility.
--JaroslavTulach 22:14, 16 January 2011 (UTC)