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.



Core Java Data Objects
Core Java Data Objects
ISBN: 0131407317
EAN: 2147483647
Year: 2003
Pages: 146

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net