2

I'm trying to create a StandardCard class which uses enums for suits and numbers. I have the code as it is currently below.

My problem is that the enums should be accessible to the Driver class which is adding cards to the deck. Having read about design patterns recently I feel that I should have a StandardCardEnums class and create an object of this class within the StandardCard class to add modularity. Is this the way to go? It feels clunky as I then need to do something like

StandardCardEnum myEnums = new StandardCardEnum;
private StandardCardEnum.Suit suit;
etc.....

My current code is below, the issues I see with it are 1) The enums are hard-coded within the card class, duplication is definitely going to happen 2) The Driver class needs to know about the enums to populate cards in the deck hence violating object encapsulation.

Can anyone with more experience point me towards a good standard to start applying to these types of situations? I'm trying to move away from programming towards software development. I'm hoping to be able to create a modular card-game framework that will allow me to add in different card games and even deck compositions and card types with a minimum of effort.

public class StandardCard extends Card
{
public enum Suit {CLUBS, DIAMONDS, HEARTS , SPADES };
public enum Number {ACE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING};

private Suit suit;
private Number number;

public StandardCard(StandardCard.Suit suit, StandardCard.Number number)
{
    this.suit = suit;
    this.number = number;
}

public StandardCard()
{
}

public Suit getSuit()
{
    return suit;
}

public Number getNumber()
{
    return number;
}

public void setSuit(Suit s)
{
    this.suit = s;
}

public void setNumber(Number n)
{
    this.number = n;
}
}
LiamRyan
  • 1,892
  • 4
  • 32
  • 61
  • 2
    1) Your enums are already accessible from anywhere, they just require the outer class name as a qualifier. If you don't like that, don't make the enums nested. 2) If you want to be able to keep the potentical card types open-ended, then enums are not the way to go. – Kirk Woll Feb 08 '13 at 00:32
  • Kirk, I think you can definitely rework this comment as a full answer, since you are ansewring the question, you are basically right, and also giving additional value to the original question :-) – dunadar Feb 08 '13 at 00:44
  • @Kirk Could you give an example of nesting the enums, would that involve much duplication? Additionally my idea is that I have an abstract Card superclass which has subclasses of standardcard, magicthegatheringcard etc. and the relevant enums just plug in to those subclasses – LiamRyan Feb 08 '13 at 00:48
  • 1
    @GaborSch, [you are mistaken](http://stackoverflow.com/questions/4827326/are-enum-types-declared-in-a-class-implicitly-static). – Kirk Woll Feb 08 '13 at 01:54
  • 1
    @GaborSch, did you read the link? Enums are *always* implicitly static. There's no such thing as a non-static enum. – Kirk Woll Feb 08 '13 at 02:03
  • Something new I learned again, thanks! – gaborsch Feb 08 '13 at 02:05

2 Answers2

2

Put the enums into their own files as top-level classes.

user207421
  • 305,947
  • 44
  • 307
  • 483
1

You should rather make your enums static:

public static enum Suit {CLUBS, DIAMONDS, HEARTS , SPADES };
public static enum Number {ACE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING};

This way you can access them as StandardCard.Suit.CLUBS from anywhere in your code (e.g. from Driver), but they're still encapsulated into StandardCard - they have no meaning outside this world.

gaborsch
  • 15,408
  • 6
  • 37
  • 48
  • This solves my main issue and thank you for that. The other answers were also helpful but it's always nice to see examples and a justification. – LiamRyan Feb 08 '13 at 00:56
  • The design for a `Card` and `StandardCard` is nice, +1 for that. This way you can implement a `HelveticCard`, `TarotCard`, etc - different card types, all of them can enclose the logic related to them. – gaborsch Feb 08 '13 at 01:01
  • 1
    This answer accomplishes nothing. [Nested enum types are implicitly static.](http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.9) -1 – user207421 Feb 08 '13 at 02:01