(转自
http://www.blogjava.net/dazuiba/archive/2006/08/04/j2se_enum.html)
1 定义在常量类中    
    经常碰到要将枚举类当成常量使用的情况,这不仅可以将相关的常量定义到一个枚举类中,而且还可以利用枚举类强大而又灵活的功能,在加上编译器内置的支持,使得在eclipse下的编程更方便,引入的bug更少。
    一般规模的项目中都会用一个单独的类来定义系统中用到的常量,起码笔者经历的几个项目都是有此种做法,该做法的好处就是便于集中管理,虽然这违背类封装的原则,但鉴于其易用性,我们还是会常常这么做。
    例子:


 public class SystemConstant
public class SystemConstant  {
{

 /** *//**
    /** *//**
 * 金库 sourceortarget 系统相关
     * 金库 sourceortarget 系统相关
 */
     */
 public static final String CASHWASTEBOOK_SOURCEORTARGET_SYS = "系统";
    public static final String CASHWASTEBOOK_SOURCEORTARGET_SYS = "系统";

 /** *//**
    /** *//**
 * 附件上传路径
     * 附件上传路径
 */
     */ 
 public static String UPLOAD_ATTACHMENT_DIR="upload\\";
    public static String UPLOAD_ATTACHMENT_DIR="upload\\";
 public static String CONFIG_DIR="config\\";
    public static String CONFIG_DIR="config\\";

 /** *//**
    /** *//**
 * 临时文件路径
     * 临时文件路径
 */
     */
 public static String TEMP_DIR="temp\\";
    public static String TEMP_DIR="temp\\";

 /** *//**
    /** *//**
 * 会员关系
     * 会员关系
 */
     */

 public static enum Relationship
    public static enum Relationship  {
{
 GoodFriend("亲密好友"),
        GoodFriend("亲密好友"),
 CommonFriend("普通朋友"),
        CommonFriend("普通朋友"),
 BLACK("不受欢迎");
        BLACK("不受欢迎");
 private   String v;
        private   String v;
 
        

 Relationship(String value)
        Relationship(String value)  {
{
 v = value;
          v = value;
 }
        }
 @Override
        @Override

 public String toString()
        public String toString()  {
{        
 return v;
            return v;
 }
        } 
 }
      }  
 public static final String SUCCESS = "OK";
    public static final String SUCCESS = "OK";

 /** *//**用户选择管理员登录*/
    /** *//**用户选择管理员登录*/
 public static final String MESSAGE_LOGIN_TYPEERROR1 = "您不能选择管理员登录";
    public static final String MESSAGE_LOGIN_TYPEERROR1 = "您不能选择管理员登录";

 /** *//**管理员选择会员或家长登录*/
    /** *//**管理员选择会员或家长登录*/
 public static final String MESSAGE_LOGIN_TYPEERROR2 = "您应该选择管理员登录";
    public static final String MESSAGE_LOGIN_TYPEERROR2 = "您应该选择管理员登录";

 /** *//**会员或家长重复登陆*/
    /** *//**会员或家长重复登陆*/
 public static final String MESSAGE_LOGIN_REPEAT = "可能因为以下原因,您无法登陆系统\n\t1 有人盗用您的帐号\n2 您的{0}正在使用本帐号";
    public static final String MESSAGE_LOGIN_REPEAT = "可能因为以下原因,您无法登陆系统\n\t1 有人盗用您的帐号\n2 您的{0}正在使用本帐号";
 public static final String MESSAGE_LONGIN_PASSWORDERROR = "用户名或密码无效";
    public static final String MESSAGE_LONGIN_PASSWORDERROR = "用户名或密码无效";
 public static final String MESSAGE_INSUFFICIENT_FUNDS = "您的帐户余额不足";
    public static final String MESSAGE_INSUFFICIENT_FUNDS = "您的帐户余额不足";
 public static final String MESSAGE_MEMBER_ONLINETIME_FULL = "您今日的累计上线时间已超过1.5小时";
    public static final String MESSAGE_MEMBER_ONLINETIME_FULL = "您今日的累计上线时间已超过1.5小时";

 /** *//**会员每天最大登录时限 单位分钟 默认90**/
    /** *//**会员每天最大登录时限 单位分钟 默认90**/
 public static final int MEMBER_MAX_DAY_ONLINE_MINUTES = 90;
    public static final int MEMBER_MAX_DAY_ONLINE_MINUTES = 90;
 
    
 }
}

    可以看到,枚举类型Relationship是定义一些会员关系之间的东东,其实我可以把它单独定义一个类,或者放到Member(会员)这个类中,但综合考虑,我还是觉得放到SystemConstant比较好,并且今后重构SystemConstant,会添加从xml文件读取属性的功能。
     虽然Relationship是一个内部类,但由于是静态的,所以可以直接import,而无须每次都用SystemConstant.Relationship;
 例如:
    public Relationship getRelationship() {
           return Relationship.valueOf(relationship);
        }
  2 说到从xml文件读取属性来动态配置枚举类,我下面就举个例子,演示演示
     一些web系统中涉及到文件上传,根据文件类型显示相应图标,并且有些jsp,asp等等的文件不允许上传,下面就是一个满足这种需求的枚举类,它最大的特点就是可以从xml中读取配置信息

 /** *//**
/** *//**
 * 系统中用到的文件扩展名 枚举类
 * 系统中用到的文件扩展名 枚举类
 * @author zgy
 * @author zgy
 *
 *
 */
 */

 public enum FileExtension
public enum FileExtension  {
{
 doc, jsp, jpeg, jpg, rar, zip, txt,unknown;
    doc, jsp, jpeg, jpg, rar, zip, txt,unknown;

 private boolean allow;
    private boolean allow;
 
 

 private String comment;
    private String comment;

 private String iconPath;
    private String iconPath;

 static
    static  {
{
 loadFromXml();
        loadFromXml(); 
 }
    } 

 FileExtension()
    FileExtension()  {
{
 this.iconPath = "\\" + name();
        this.iconPath = "\\" + name();
 this.allow = true;
        this.allow = true;
 this.comment = "comment for" + name();
        this.comment = "comment for" + name();
 }
    }    

 /** *//**
    /** *//**
 * 从config目录中load
     * 从config目录中load
 *
     * 
 */
     */

 private static void loadFromXml()
    private static void loadFromXml()  {
{

 try
        try  {
{
 Document doc = XmlUtil.parseXmlFile(SystemConstant.CONFIG_DIR
            Document doc = XmlUtil.parseXmlFile(SystemConstant.CONFIG_DIR
 + "fileExtension.xml");
                    + "fileExtension.xml");
 NodeList extensionList = doc.getElementsByTagName("FileExtension");
            NodeList extensionList = doc.getElementsByTagName("FileExtension");

 for (int i = 0; i < extensionList.getLength(); i++)
            for (int i = 0; i < extensionList.getLength(); i++)  {
{
 Element item = (Element) extensionList.item(i);
                Element item = (Element) extensionList.item(i);
 String name = item.getAttribute("name");
                String name = item.getAttribute("name");
 FileExtension em = FileExtension.valueOf(name);
                FileExtension em = FileExtension.valueOf(name);
 em.allow = Boolean.parseBoolean(item.getAttribute("allow"));
                em.allow = Boolean.parseBoolean(item.getAttribute("allow"));
 em.iconPath = item.getAttribute("iconPath");
                em.iconPath = item.getAttribute("iconPath");
 em.comment = item.getAttribute("comment");
                em.comment = item.getAttribute("comment"); 
 }
            }

 } catch (Exception e)
        } catch (Exception e)  {
{ 
 throw new RuntimeException(e);
            throw new RuntimeException(e);
 }
        }
 }
    }


 public boolean isAllow()
    public boolean isAllow()  {
{
 return allow;
        return allow;
 }
    }


 public String getComment()
    public String getComment()  {
{
 return comment;
        return comment;
 }
    } 

 public String getUploadIcon()
    public String getUploadIcon()  {
{
 return iconPath;
        return iconPath;
 }
    }


 public static void main(String[] args)
    public static void main(String[] args)  {
{
 System.out.println(FileExtension.doc.comment);
        System.out.println(FileExtension.doc.comment);
 }
    }
 }
}

配置文件如下:config/fileExtension.xml
<?xml version="1.0" encoding="UTF-8"?>
<FileExtensions> 
 <FileExtension name="doc" iconPath="doc.jpg" allow="true"   comment="文本"/>
 <FileExtension name="jpg" iconPath="jpg.jpg" allow="true"   comment=""/>
 <FileExtension name="jpeg" iconPath="jpeg.jpg" allow="true" comment=""/>
 <FileExtension name="rar" iconPath="rar.jpg" allow="true"   comment=""/>
 <FileExtension name="zip" iconPath="zip.jpg" allow="true"   comment=""/>
 <FileExtension name="txt" iconPath="txt.jpg" allow="true"   comment=""/>
    <FileExtension name="jsp" iconPath="jsp.jpg" allow="false"  comment=""/>
</FileExtensions>
可能系统中其他的一些枚举类(比如1 中提到的RelationShip)也会用到非常类似的做法,这时候我们就可以重构了,将一些共同的特点抽取到一个抽象类中。这将会在以后的文章中提到。
有不同的观点,请联系
come2u at gmail.com  ,欢迎交流。