patternjavaMinor
Should I design my enumeration in some way that indicates what the highest value is?
Viewed 0 times
enumerationthewhatindicatesdesignwayvaluethatsomeshould
Problem
This is a subtle question about design in which I want to find the most elegant and appropriate solution.
I have an enumeration representing a French card deck (see code below). With it I need to do certain things, such as displaying the elements in a table. I'll want to display the elements starting with ACE (the highest) down to deuce. The problem is that I don't want my UI part of the code to know which one is the highest and use a descending iterator. Should I have a method in my enumeration, something like "EnumSet descending()" ? I don't want to initialise the constants with integers that indicates their values, since in poker the cards don't have a numerical value per se, just an order. Probably it's just fine if users of this enum are supposed to know that an ACE has the highest value and display values accordingly to what they want, but just wondering if there is a better solution.
I have an enumeration representing a French card deck (see code below). With it I need to do certain things, such as displaying the elements in a table. I'll want to display the elements starting with ACE (the highest) down to deuce. The problem is that I don't want my UI part of the code to know which one is the highest and use a descending iterator. Should I have a method in my enumeration, something like "EnumSet descending()" ? I don't want to initialise the constants with integers that indicates their values, since in poker the cards don't have a numerical value per se, just an order. Probably it's just fine if users of this enum are supposed to know that an ACE has the highest value and display values accordingly to what they want, but just wondering if there is a better solution.
public enum Rank {
DEUCE ("2"),
THREE ("3"),
FOUR ("4"),
FIVE ("5"),
SIX ("6"),
SEVEN ("7"),
EIGHT ("8"),
NINE ("9"),
TEN ("10"),
JACK ("J"),
QUEEN ("Q"),
KING ("K"),
ACE ("A");
private String symbol;
Rank (String symbol ) {
this.symbol = symbol;
}
/**
* Returns a string representation of this rank which is the full name
* where the first letter is capitalised and the rest are lowercase
*/
@Override public String toString () {
//only capitalize the first letter
String s = super.toString();
return s.substring (0, 1).toUpperCase() + s.substring(1).toLowerCase();
}
public String getSymbol () {
return this.symbol;
}
}Solution
I think you should add a numeric field indicating the order of a card relative to the other cards. You do not need to use it for anything other than sorting.
Also, instead of taking the identifier of the enum value and doing uppercase and lowercase tricks with it in order to turn it into something presentable to the user, you should just store the name, too, as a separate string in the enum.
So, it would look like this:
Furthermore you can also add one more number, the 'weight' of a card, to use in calculations which determine whether a card beats another. In general, enums in java are very powerful, so powerful that you pretty much end up not having to use the switch statement with enums anymore, because you can include a big part of an enum's functionality within the enum itself.
Also, instead of taking the identifier of the enum value and doing uppercase and lowercase tricks with it in order to turn it into something presentable to the user, you should just store the name, too, as a separate string in the enum.
So, it would look like this:
ACE( "A", "Ace", 0 ) (where 0 indicates lowest order for sorting.)Furthermore you can also add one more number, the 'weight' of a card, to use in calculations which determine whether a card beats another. In general, enums in java are very powerful, so powerful that you pretty much end up not having to use the switch statement with enums anymore, because you can include a big part of an enum's functionality within the enum itself.
Context
StackExchange Code Review Q#7593, answer score: 3
Revisions (0)
No revisions yet.