1.写个简单的证件类型转换的充血枚举
调用处
TransitionCertiUtils.setTypeDesc(certiType,application);
2.工具类
public class TransitionCertiUtils { private static final CertiType c = CertiType.getInstance(); //中文描述转换为码值 public static void setTypeDesc(String type, Application application){ TransCertiType.valueOf(c.certiMap.get(type)).getInteface().transCertiType(application); } }
3.单例模式创建并初始化证件类型的map集合
public class CertiType { //私有静态对象,加载不做初始化 private static CertiType m_intance = null; public CoreServiceTypeMap certiMap = new CoreServiceTypeMap(); //构造方法,给map设置初始值 private CertiType(){ setTypeForCerti(); } //静态方法创建实体类 synchronized public static CertiType getInstance(){ if (m_intance == null){ m_intance = new CertiType(); } return m_intance; } //设置初始值 private void setTypeForCerti() { certiMap.put("1","one");//1营业执照号码 certiMap.put("2","two");//2组织机构代码 } }
4.枚举类的创建
public enum TransCertiType{ one("营业执照号码",TransCertiTypeEnumInterface.byTransOne()), two("组织机构代码",TransCertiTypeEnumInterface.byTransTwo()), String memo; TransCertiTypeEnumInterface inteface; TransCertiType(String memo, TransCertiTypeEnumInterface inteface) { this.memo = memo; this.inteface = inteface; } //setter和getter方法... }
5.枚举方法的内部实现
public interface TransCertiTypeEnumInterface { public void transCertiType(Application application); public static TransCertiTypeEnumInterface byTransOne() { return new TransCertiTypeEnumInterface() { @Override public void transCertiType(Application application) { application.setCertTypeDesc("营业执照号码"); } }; } ; public static TransCertiTypeEnumInterface byTransTwo() { return new TransCertiTypeEnumInterface() { @Override public void transCertiType(Application application) { application.setCertTypeDesc("组织机构代码"); } }; } ; }
6.总结,使用充血枚举可以把大量不同码值对应不同操作的代码规整,适用于多平台复用同一套代码等情况