Enum type이 Typesafe enum pattern 보다 거의 모든 상황에서 우수하다.
단, 예외가 하나 있으니, 타입 안전 열거 패턴은 확장이 가능하고 열거 타입은 불가능하다는 점
타입 안전 열거 패턴은 값을 더 추가하여 다른 목적으로 쓸 수 있지만, 열거 타입은 불가능하다.
타입 안전 열거 패턴의 예제 코드
// 선언
public final class Suit // Should not be able to subclass Suit.
{
public static final Suit CLUBS = new Suit();
public static final Suit DIAMONDS = new Suit();
public static final Suit HEARTS = new Suit();
public static final Suit SPADES = new Suit();
private Suit() {} // Should not be able to introduce additional constants.
}
----------------------------------------------------------------
// 사용
Suit suit = Suit.DIAMONDS;
주석을 해석해보면
장점