Advertisement
Guest User

ES Code Discussion

a guest
Sep 30th, 2011
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. Even in the card example the bare int approach breaks down rather quickly.
  2.  
  3. I'll replace "card" with "entity"
  4.  
  5. entity.getAs(Card.class).suit -> which is 3;
  6.  
  7. In your code are you going to do what with 3? What is "3" anyway. Is it hearts, clubs, or diamonds? Nope it's spades.
  8.  
  9. switch (entity.getAs(Card.class).suit)
  10. {
  11. case 3:
  12. switch (entity.getAs(Card.class).rank)
  13. {
  14. case 0:
  15. <do something with ace of spades or is it? >
  16. break;
  17. }
  18. break;
  19. }
  20.  
  21. or some horrendous if / else block comparing both suit / rank in each conditional.
  22.  
  23. If you want to then define static types; IE
  24.  
  25. public abstract class CardDef
  26. {
  27. public static final int CLUBS = 0;
  28. public static final int DIAMONDS = 1;
  29. public static final int HEARTS = 2;
  30. public static final int SPADES = 3;
  31.  
  32. public static final int ACE = 0;
  33. public static final int TWO = 1;
  34. ...
  35. public static final int KING = 12;
  36. }
  37.  
  38. and replace the hard coded ints in the switch statement with the the above separate definitions you've now made it a little more legible, but still have the nested mess and the potential for error handling needs to be explicitly enforced all over the place as it would be a tragedy if at some point a cards suit was set to 3485891 and rank to 123895912; for whatever reason.
  39.  
  40. or does this make more sense:
  41.  
  42. switch (entity.getAs(FrenchDeckCard.class))
  43. {
  44. case ACE_OF_SPADES:
  45. <do something obvious here>
  46. break;
  47. }
  48.  
  49. one can also specifically get the rank / suit separately:
  50.  
  51. entity.getAs(FrenchDeckCard.class).suit == SPADES
  52.  
  53. how is this OOP?
  54.  
  55. public enum FrenchCardDeck implements ICOPExtensibleEnum
  56. {
  57. public enum Suit { CLUBS, HEARTS, DIAMONDS, SPADES }
  58. public enum Rank { ACE, TWO, THREE, FOUR ... KING }
  59.  
  60. ACE_OF_CLUBS(CLUBS, ACE),
  61. TWO_OF_CLUBS(CLUBS, TWO),
  62. ...
  63. KING_OF_SPADES(SPADES, KING);
  64.  
  65. public final Suit suit;
  66. public final Rank rank;
  67.  
  68. FrenchCardDeck(Suit suit, Rank rank)
  69. {
  70. this.suit = suit;
  71. this.rank = rank;
  72. }
  73. }
  74.  
  75.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement