Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Even in the card example the bare int approach breaks down rather quickly.
- I'll replace "card" with "entity"
- entity.getAs(Card.class).suit -> which is 3;
- 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.
- switch (entity.getAs(Card.class).suit)
- {
- case 3:
- switch (entity.getAs(Card.class).rank)
- {
- case 0:
- <do something with ace of spades or is it? >
- break;
- }
- break;
- }
- or some horrendous if / else block comparing both suit / rank in each conditional.
- If you want to then define static types; IE
- public abstract class CardDef
- {
- public static final int CLUBS = 0;
- public static final int DIAMONDS = 1;
- public static final int HEARTS = 2;
- public static final int SPADES = 3;
- public static final int ACE = 0;
- public static final int TWO = 1;
- ...
- public static final int KING = 12;
- }
- 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.
- or does this make more sense:
- switch (entity.getAs(FrenchDeckCard.class))
- {
- case ACE_OF_SPADES:
- <do something obvious here>
- break;
- }
- one can also specifically get the rank / suit separately:
- entity.getAs(FrenchDeckCard.class).suit == SPADES
- how is this OOP?
- public enum FrenchCardDeck implements ICOPExtensibleEnum
- {
- public enum Suit { CLUBS, HEARTS, DIAMONDS, SPADES }
- public enum Rank { ACE, TWO, THREE, FOUR ... KING }
- ACE_OF_CLUBS(CLUBS, ACE),
- TWO_OF_CLUBS(CLUBS, TWO),
- ...
- KING_OF_SPADES(SPADES, KING);
- public final Suit suit;
- public final Rank rank;
- FrenchCardDeck(Suit suit, Rank rank)
- {
- this.suit = suit;
- this.rank = rank;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement