/*******************************************************************************
* Companion code for the book "Introduction to Software Design with Java",
* 2nd edition by Martin P. Robillard.
*
* Copyright (C) 2022 by Martin P. Robillard
*
* This code is licensed under a Creative Commons
* Attribution-NonCommercial-NoDerivatives 4.0 International License.
*
* See http://creativecommons.org/licenses/by-nc-nd/4.0/
*
*******************************************************************************/
package e2.chapter2;
/**
* Represents the rank of a playing card.
*/
public {
ACE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING
}
We can be sure that there won't be any other instance of the enum because:
final
) and are static
;We can be sure that there won't be any other instance of the enum because:
final
) and are static
;The type Rank
represents, naturally, the rank of a playing card. Because there
are only 13 ranks in a standard 52-card deck, Java's enum is a good option to
represent this concept.
The type Rank
represents, naturally, the rank of a playing card. Because there
are only 13 ranks in a standard 52-card deck, Java's enum is a good option to
represent this concept.
It is generally a poor design choice to rely on primitive types (e.g., int
,
boolean
, and including String
) to represent unrelated concepts.
For example, while it might be tempting to represent a Rank by an int
ranging from 1 to 13, the correlation between the number and the rank doesn't
hold very well, especially for figure cards (jacks, queens, and kings).
Furthermore, because there are a lot more legal integer values than valid ranks, it's easy to assign to the variable representing the rank an invalid value, thus corrupting the object.
Instead, we can create meaningful types, like Rank
, which clearly represent
a concept from the problem domain, and can later be modified if needed.
It is generally a poor design choice to rely on primitive types (e.g., int
,
boolean
, and including String
) to represent unrelated concepts.
For example, while it might be tempting to represent a Rank by an int
ranging from 1 to 13, the correlation between the number and the rank doesn't
hold very well, especially for figure cards (jacks, queens, and kings).
Furthermore, because there are a lot more legal integer values than valid ranks, it's easy to assign to the variable representing the rank an invalid value, thus corrupting the object.
Instead, we can create meaningful types, like Rank
, which clearly represent
a concept from the problem domain, and can later be modified if needed.
Values of an enum are unique: two objects of the same enum type are equal if and only if they are the same object. In other words, two references are equal if they point to the same memory allocation.
As a result, we can compare variables of an enum type using the ==
operator,
rather than the Object.equals(Object)
method, although the latter would also
work.
Values of an enum are unique: two objects of the same enum type are equal if and only if they are the same object. In other words, two references are equal if they point to the same memory allocation.
As a result, we can compare variables of an enum type using the ==
operator,
rather than the Object.equals(Object)
method, although the latter would also
work.
In Java, an enum
, short for "enumerated type", is a special kind of class that
defines a finite set of constants, which are
of this class. These constants are visible anywhere in the code (i.e., public
visibility).
An enum is useful to represent abstractions that have a finite number of realizations, which are known in advance.
Enums are also good ways to avoid the antipattern.
In Java, an enum
, short for "enumerated type", is a special kind of class that
defines a finite set of constants, which are
of this class. These constants are visible anywhere in the code (i.e., public
visibility).
An enum is useful to represent abstractions that have a finite number of realizations, which are known in advance.
Enums are also good ways to avoid the antipattern.
The list of values of an enum, i.e., all possible instances, must be declared at the top of the enum declaration. In this case, there are only 13 values.
As common with constants (i.e., static final
fields), they are declared in
all uppercase, using underscores to split words.
The list of values of an enum, i.e., all possible instances, must be declared at the top of the enum declaration. In this case, there are only 13 values.
As common with constants (i.e., static final
fields), they are declared in
all uppercase, using underscores to split words.