Java Enum in details

Java 5 has brought linguistic support for enumerated types. An enum type is a type whose fields consist of a fixed set of constants. The enum provides following features
  • Typesafe
  • Namespace
  • Printable constants values
Typical and often enum is being used to replace int constants. For example constants for color codes
public static final int RED = 0;
public static final int GREEN = 1;
public static final int BLUE = 3;
public static final int WHITE =4;
public static final int BLACK = 6;
 

This code can be replaced by following enum
public enum StandardColor {
    RED,GREEN,BLUE,WHITE,BLACK
}

Given the above enum declared, we can use the same to communicate the color preferences. For example we have Window class and it supports setting background color using setBackgroundColor(StandardColor color). One can call setBackgroundColor method on Window's instance to set background color, as shown in code below
public class Window{
    //..
    public setBackgroundColor(StandardColor color){
        if(color == StandardColor.Black){
            setBackgroundColor(#000000);
        } else if(color == StandardColor.WHITE){
            setBackgroundColor(#FFFFFF);
        }
        //..
    }
}


Windows myWindow = new Window();
myWindow.setBackgroundColor(StandardColor.BLACK);

Everything will go smooth till you have same understanding between StandardColor enum elements declaration and its implementers. Now consider the case you have to declare another color entry called “MY_COLOR” and corresponding html code will be#0000A0”. To let the corresponding html code we may document in comment.
enum StandardColor {
    BLACK,WHITE,RED,GREEN,BLUE,
    /**
     * HTML Code #0000A0
     */
    MY_COLOR
}

Addition to that we need to modify setBackgroundColor() method
public setBackgroundColor(StandardColor color){
    if(color == StandardColor.Black){
        setBackgroundColor(#000000);
    } else if(color == StandardColor.WHITE){
        setBackgroundColor(#FFFFFF);
    } else if(color == StandardColor.MY_COLOR){
        setBackgroundColor(#0000A0);
    }
    //..
}

Here is the problems, it you modify the color code, then all corresponding needs to be changed.
Java programming language enum types are much more powerful, the enum class body can include methods and other fields.Each enum constants can be declared with values. These values are passed to the constructor when constants are created. Enum StandardColor constants with RGB values to represent its actual value will make it self explanatory.
enum StandardColor {
    RED(255,0,0),
    GREEN(0,255,0),
    BLUE(0,0,255),
    WHITE(255,255,255), 
    BLACK(0,0,0) ;
    final int r,g,b;
    private StandardColor(int rParam, int gParam, int bPram){
        r = rParam;
        g = gParam;
        b = bPram;
    }
    
    public String getHexValue(){
        return "#"+ Integer.toHexString(r)+Integer.toHexString(g)+Integer.toHexString(b);
    }
    
    private String intToHex(int i){
        return Integer.toHexString(i);
    }
}
Here enum StandardColor constants accept three integer representing red, green and blue values while declaring it. As seen in code above , it has private constructor with three integers and a public method getHexValue() to get html value of color. Now the Windows.setBackgroundColor(StandardColor) method will be much more simpler that previous version.
public setBackgroundColor(StandardColor color){
    setBackgroundColor(color.getHexValue());
}
Now doesn’t matter what modification you do in StandardColor enum like introducing new constants or changing constant’s value, all dependents need not to be cared.

Comments

Popular posts from this blog

State Design Pattern by Example

Eclipse command framework core expression: Property tester

Composite Design Pattern by example