Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!.POSTED!not-for-mail From: markspace <-@.> Newsgroups: comp.lang.java.programmer Subject: Re: proper use of .java files (layout) Date: Mon, 17 Dec 2012 13:17:53 -0800 Organization: A noiseless patient Spider Lines: 54 Message-ID: References: <20fa5c05-6fcc-47ed-9e80-a44975887928@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Mon, 17 Dec 2012 21:17:55 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="61282af8d6595e8d991edb5ac03d6e00"; logging-data="26293"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19wA4CbVupIVZRDY/0oMIkgUF1ZpjxOCJA=" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/17.0 Thunderbird/17.0 In-Reply-To: <20fa5c05-6fcc-47ed-9e80-a44975887928@googlegroups.com> Cancel-Lock: sha1:gazJaORMMRaSuF54xhGytkIpdCE= Xref: csiph.com comp.lang.java.programmer:20398 On 12/17/2012 10:28 AM, Lew wrote: > markspace wrote: >> public class PlayingCard { >> >> private final int value; // Ace = 1 > > I would make this an enum. > > "Ace = 1" is not a valid association in the problem domain, and its use > as a representation creates risk of an artificial equivalency. I was actually thinking about that. I don't like the idea of trying to encode most of the values of a face card as enums. Something like this might be a reasonable compromise. public static enum Cards {ACE, DEUCE, KING, QUEEN, JACK } public void set( Cards card, Suit suit ) { switch( card ) { case ACE: value = 1; break; case DEUCE: value = 2; break; case KING: value = 13; break; case QUEEN: value = 12; break; case JACK: value = 11; break; } this.suit = suit; } public void set( int card, Suit suit ) { value = card; this.suit = suit; } I also think it might be valuable to have one implementation of PlayingCard with one internal encoding, then assigning different semantics (e.g., ace high or low) in a separate game object. Trying to overload PlayingCard too much with too much business logic seems to violate encapsulation. A simple wrapper class could easily switch the value of an ace; so could a custom comparator. Without more requirements from the OP, it's really hard to guess what of the many possible solutions is best. So I went with a simple implementation of "ace" (ace = 1) and I'm leaving to other logic to decide how to treat that. You could have other more complicated implementations of PlayingCard, but too much gets too baroque quickly, imo, especially if you don't need all that functionality for most use cases. Another example: in many standard decks, the names of the suits are not Spades, Hearts, etc. but vary by country. Again I'm leaving that for some other part of the program. Localization of card names can be done by wrapping this PlayingCard with a GermanPlayingCard, for example.