Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Yeah I'd model it as something like this maybe?

  public enum Color {
    RED, BLACK
  }

  public enum Suit {
    Diamonds(RED, '♦'),
    Hearts(RED, '♥'),
    Clubs(BLACK, '♣'),
    Spades(BLACK, '♠');

    Color color;
    char symbol;

    public Suit(Color color, char symbol) { this.color = color; this.symbol = symbol; }
  }

  public enum Rank {
    Ace('A'),
    Two('2'),
    //...
  }

  public record Card(Suit suit, Rank rank) {
     // ... 
  }
The question is fundamentally broken because data objects shouldn't be inheriting anything. That's in almost all cases bad design that demonstrates only that you have no clue how to write sensible object-oriented code.

You wouldn't want to check whether a poker hand has a pair by using a bunch of instanceof's or getClass()-shenanigans. You also don't want to encode knowledge about poker into into the card object. That's just data.



Nice. Very thorough, that’s a more positive take than what I was presenting, though I feel we are both (rightly) being standoffish on the whole inheritance requirement.


Hey, the client just said they want to play with Jokers can you add this in by lunch for the demo?

Thanks!


In that case I'd probably add a Joker Rank and Suit and require in Card's constructor that either both fields say Joker, or none do.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: