14.6. Enumeration Pattern

The enumeration pattern, where specific instances of a Java class represent the allowed values in an enumeration, is a useful programming technique in Java to constrain the values that can be assigned to a field.

As an example of this pattern, the following code snippet shows how the enumeration pattern can be used to represent the allowable eye colors for a person:

public class Person {

  private EyeColor eyes = Color.BLUE;

  /* Rest of code not shown */
}

public class EyeColor {

  public static EyeColor BLUE  = new Color("blue");
  public static EyeColor BROWN = new Color("brown");

  private String color;

  private EyeColor(String color) {

    this.color = color;
  }
}

JDO 2.0 may add explicit support for this pattern so that applications can use enumerations as fields of persistent objects.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset