Enum使用经验:
1.Enum类型本身实现了序列化,可以随着对象放到memcache;
2.通常每个Enum需要有一个int类型的属性。DB中保存的字段可以是一个enum的值,也可以多个enum的与或的结果。比字符串的扩展性更好。
2.1 type的子类型的设计
注意,通常权限都是1, 2, 4, 8..... 二进制值11仍然可以表示为10的仅有的子类型。101,110,111可以表示100的三种子类型。
如果type的值存在子类型值,那么sql中就不适合通过type = someType 来获取包含子类型的所有记录
假如库中,type=1表示想去,type=2表示去过, type=1 |2表示从想去到运过的状态(但属于去过的一种子类型),如果想查询所有的去过的记录,type & 2 >0就是正确的办法。
2.2 在sql中使用 type & allType=someType与type=someType效果类似。
3.使用Map代替switch case 返回对应的枚举。一种好的方式是在enum中的static块中构造map。
public static enum PostCategory { NO(0, "_post_category_no", ""), TRIP(1, "_post_category_lvyouluxian", "Lvyouluxian"), FOOD( 2, "_post_category_meishi", "Meishi"), SHOPPING(3, "_post_category_gouwu", "Gouwu"), ENTAINMENT(4, "_post_category_yule", "Yule"), OTHER(5, "_post_category_qita", "Qita"); private static final Map<Integer, PostCategory> mLookup = new HashMap<Integer, PostCategory>(); static { for (PostCategory category : EnumSet.allOf(PostCategory.class)) { mLookup.put(category.getFlag(), category); } } public static PostCategory get(int nCategoryId) { return mLookup.get(nCategoryId); } private int m_nFlag; private String m_sCaption; private String m_sPinyin; private PostCategory(int nFlag, String sCaption, String sPinyin) { m_nFlag = nFlag; m_sCaption = sCaption; m_sPinyin = sPinyin; } public int getFlag() { return m_nFlag; } public String getCaption() { return m_sCaption; } public String getPinyin() { return m_sPinyin; } }
4.对于一些存在关联关系的enum,static块中也可以使用map去映射关系。
public enum SubCategory { YAN_TU_JING_DIAN(PostCategory.TRIP, "subcategory6_tag", "subcategory_yantujingdian", 6), XIAN_LU_TE_SE( PostCategory.TRIP, "subcategory7_tag", "subcategory_xianlutese", 7), TUI_JIAN_CAN_GUAN( PostCategory.FOOD, "subcategory8_tag", "subcategory_tuijiancanguan", 8), DANG_DI_MEI_SHI( PostCategory.FOOD, "subcategory9_tag", "subcategory_dangdimeishi", 9), GOU_WU_CHANG_SU( PostCategory.SHOPPING, "subcategory10_tag", "subcategory_gouwuchangsu", 10), SU_GOU_SHANG_PIN( PostCategory.SHOPPING, "subcategory11_tag", "subcategory_sugoushangpin", 11), YU_LE_CHANG_SUO( PostCategory.ENTAINMENT, "subcategory12_tag", "subcategory_yulechangsuo", 12); private static Map<PostCategory, List<SubCategory>> CATEGORY_MAP = new HashMap<DaoDaoTourismMetadata.PostCategory, List<SubCategory>>(); private static List<String> NAME_SET = new ArrayList<String>(); static { for (SubCategory category : EnumSet.allOf(SubCategory.class)) { { if (CATEGORY_MAP.get(category.getParentCategory()) == null) { List<SubCategory> scl = new ArrayList<SubCategory>(); scl.add(category); CATEGORY_MAP.put(category.getParentCategory(), scl); } else { CATEGORY_MAP.get(category.getParentCategory()).add(category); } NAME_SET.add(category.getName()); } } } private PostCategory parentCategory; private String formField; private String key; private int id; private SubCategory(PostCategory parent, String field, String name, int id) { this.parentCategory = parent; this.formField = field; this.key = name; this.id = id; } public String getFieldName() { return this.formField; } public String getName() { return UserInterfaceUtil.getInstance().localize(this.key); } public int getId() { return this.id; } public PostCategory getParentCategory() { return this.parentCategory; } public static List<SubCategory> getSubCategoryBy(PostCategory category) { return CATEGORY_MAP.get(category); } public static List<String> getNameSet() { return NAME_SET; } }
5.注意Enum类的equal方法是不希望用户去复盖,使用了final,默认是比较引用。