Код: Выделить всё
/* Card */
public abstract class AbstractCard {
}
// Standard card
public final class StandardCard extends AbstractCard {
private final Suit suit;
private final Rank rank;
}
// I can introduce some other card. For example for Game Uno and etc.
/* Hand */
public abstract class AbstractHand {
protected final List cards;
protected HandState state;
}
// Common type of deck where you see all cards and you can choose which to use
public final class StandardHand extends AbstractHand {
public C getCard(int index) {
return cards.remove(index);
}
}
// You unable to see cards in your hand and only take the top one
public final class BlindHand extends AbstractHand {
public C getCard() {
return cards.remove(cards.size() - 1);
}
}
/* Deck */
public abstract class AbstractDeck implements Iterable {
private final List cards;
private int index;
}
// Then I can have standard(unmodifiable) deck / modifiable(allows to add cards back to deck) deck / etc.
// Further, everywhere where I want to work with Abstract + Card/Hand/Deck (btw I have also
// Board and Table) I need to provide types, like this:
private final StandardTable table;
// Or passing in method:
default void splitCardsBetweenHands(D deck, List hands) {
hands.forEach(hand -> {
while (hand.size() < 6 && deck.iterator().hasNext()) {
hand.addCard(deck.iterator().next());
}
});
}
// It results to complex method creation but gives a lot of flexibility
// but I feel that as my code grows I may encounter more nested constructions
// somehow I want to avoid that.
// I don't remember that any Java library provides example where you have to
// write type more than 2/3 times.
Подробнее здесь: https://stackoverflow.com/questions/784 ... rent-types