• 充血枚举的用法


    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.总结,使用充血枚举可以把大量不同码值对应不同操作的代码规整,适用于多平台复用同一套代码等情况

  • 相关阅读:
    MySQL数据库常见面试题
    抽象类与接口
    HashMap与Hashtable的区别
    IDEA破解
    重写equals方法
    MFC编程入门之十七(对话框:文件对话框)
    MFC编程入门之十六(对话框:消息对话框)
    MFC编程入门之十五(对话框:一般属性页对话框的创建及显示)
    MFC编程入门之十三(对话框:属性页对话框及相关类的介绍)
    MFC编程入门之十二(对话框:非模态对话框的创建及显示)
  • 原文地址:https://www.cnblogs.com/bin-zhao/p/13353703.html
Copyright © 2020-2023  润新知