Cards for Monster, Spell and Trap interfaces So I wrote a card game in Java/JavaFX but I don't particularly like this solution and I was wondering if you guys have any better solutions:

public static CardNode get(Card card, double nodeWidth, double nodeHeight, double imageWidth, double imageHeight) {
    if (card.isMonster()) {
        MonsterCardNode cardNode = new MonsterCardNode(nodeWidth, nodeHeight, imageWidth, imageHeight);
        cardNode.setCard(card);
        return cardNode;
    } else if (card.isSpell()) {
        SpellCardNode cardNode = new SpellCardNode(nodeWidth, nodeHeight, imageWidth, imageHeight);
        cardNode.setCard(card);
        return cardNode;
    } else if (card.isTrap()) {
        TrapCardNode cardNode = new TrapCardNode(nodeWidth, nodeHeight, imageWidth, imageHeight);
        cardNode.setCard(card);
        return cardNode;
    }
    return null;
}


CardNode and Card are abstract classes. They both implement interfaces Monster, Spell and Trap. Methods isMonster, isSpell, isTrap return a boolean and are essentially instanceof and flag checks. I was wondering if there is any better way to handle this with polymorphism.



Here is the usage of that static method.

model.getPlayer().getHand().addListener((ListChangeListener.Change&lt;? extends Card&gt; change) -&gt; {
    while (change.next()) {
        if (change.wasAdded()) {
            change.getAddedSubList().forEach((card) -&gt; {
            Platform.runLater(() -&gt; {
                    CardNode cardNode = CardNode.get(card, 105, 145, 96, 105);
                    view.getPlayerView().getHand().getChildren().add(cardNode);
                });
            });
        }
    }
});
As a general principle, instead of asking an object what its type is, tell it what you want to do, and let overridden methods handle the differences between types:

return card.getCardNode(nodeWidth, nodeHeight, imageWidth, imageHeight);


and add a getCardNode abstract method to Card, so that each Card subclass can implement its own piece of the get logic.