Java Performance: The Definitive Guide: Getting the Most Out of Your Code

Category: Software
Author: Scott Oaks
4.2
This Month Stack Overflow 1

Comments

by anonymous   2019-01-13

With regard to those domain models, I agree with Simon Forsberg that those are way too complicated, and I understand they're designed to demonstrate some concepts, but even then they could have used a more realistic example, like components in an automobile and their relations. You probably need only two domain classes, Card and Player. For an ordered collection of Cards, use a List<Card>. When it comes to dealing, taking turns, etc. there is a lot of room for creativity for how to organize game logic. Generally it is preferable to compose types out of other ones rather than rely heavily on inheritance which is less flexible in practice.

When it comes to performance, common rules apply. For instance, reuse objects where possible. If you're always using the 52-card deck then there's no reason to even have a Card class. There's probably nothing better-performing than reusing enumeration values everywhere.

enum Card {
    ACE_CLUBS,
    TWO_CLUBS,
    // ...
    ACE_DIAMONDS,
    // ...
}

(You can't really make this enum any more complicated, since different games use different orderings, place different values on different cards depending on the context, etc.)

For an excellent practical guide to programming for performance, I recommend the book, Java Performance: The Definitive Guide.

by gtw_jj_2   2018-03-07
https://www.amazon.com/Java-Performance-Charlie-Hunt/dp/0137...

https://www.amazon.com/Java-Performance-Companion-Charlie-Hu...

https://www.amazon.com/Java-Performance-Definitive-Guide-Get...