`

用二维数组管理好你零乱的状态、分类和其它常用选项

阅读更多
在大家的编码过程当中,有没有遇到过这么一种情况,很多零乱的状态、分类和其它常用选项常常是定义死了。但是没有一个完整的东东来约束他,在每个模块当中使用相关的信息时,往往重新COPY一次,或者COPY过来修改一次。如果多人协作的话,务必会让代码变的零乱、不好管理等。
    本次主要是把一些静态的分类、状态或者其它常用选项使用二维数组管理起来。如果你是一个使用JSTL或者STRUTS做前台表现的话,你就更应该好好关注了。
    以下以一个审核的状态做示例,使用代码实现(光说不练是假功夫).
创建Config.java
package com.antbee;

import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 * </p>
 * 
 * <p>
 * Copyright: 版权所有 (c) 2002 - 2006
 * </p>
 * <p>
 * Company: Antbee
 * </p>
 * 
 * @author @家军
 * @version 1.0
 */

public class Config {
    /**
     * 客户状态
     */
    Object vec[][] = { { new Integer(1), new String("申请"),new String("apply")},
            { new Integer(2), new String("待审核"),new String("auditing") },
            { new Integer(3), new String("签约"),new String("sign up") },
            { new Integer(4), new String("续签中"),new String("nominator") },
            { new Integer(5), new String("过期"),new String("overdue") } };
    public List Stutas = GetConfigEntity(vec);    
    public List GetConfigEntity(Object allconfig[][]) {
        List ConfigList = new ArrayList();
        ConfigEntity first = new ConfigEntity();
        first.setTypeSeri(Integer.valueOf("0"));
        first.setTypeCnName("请选择");
        first.setTypeEnName("Pls Select");
        ConfigList.add(first);
        for(int i=0;i<allconfig.length;i++){
            ConfigEntity configEntity = new ConfigEntity();            
            configEntity.setTypeSeri(Integer.valueOf(allconfig[i][0].toString()));
            configEntity.setTypeCnName(allconfig[i][1].toString());
            configEntity.setTypeEnName(allconfig[i][2].toString());
            ConfigList.add(configEntity);
        }
        return ConfigList;
    }
}

对应的PO实体类ConfigEntity.java(如果有数据库的话,你就可以直接从数据库当中取)
package com.antbee;

import java.io.Serializable;

public class ConfigEntity implements Serializable {
       /** The cached hash code value for this instance.  Settting to 0 triggers re-calculation. */
    private int hashValue = 0;

    /** The composite primary key value. */
    private java.lang.Integer typeSeri;

    /** The value of the simple typeName property. */
    private java.lang.String typeCnName;
    
    /** The value of the simple typeName property. */
    private java.lang.String typeEnName;

    /**
     * Simple constructor of AbstractDicType instances.
     */
    public ConfigEntity()
    {
    }

    /**
     * Constructor of AbstractDicType instances given a simple primary key.
     * @param typeSeri
     */
    public ConfigEntity(java.lang.Integer typeSeri)
    {
        this.setTypeSeri(typeSeri);
    }

    /**
     * Return the simple primary key value that identifies this object.
     * @return java.lang.Integer
     */
    public java.lang.Integer getTypeSeri()
    {
        return typeSeri;
    }

    /**
     * Set the simple primary key value that identifies this object.
     * @param typeSeri
     */
    public void setTypeSeri(java.lang.Integer typeSeri)
    {
        this.hashValue = 0;
        this.typeSeri = typeSeri;
    }

    /**
     * Return the value of the TYPE_NAME column.
     * @return java.lang.String
     */
    public java.lang.String getTypeCnName()
    {
        return this.typeCnName;
    }

    /**
     * Set the value of the TYPE_NAME column.
     * @param typeName
     */
    public void setTypeCnName(java.lang.String typeCnName)
    {
        this.typeCnName = typeCnName;
    }   

    /**
     * Return the value of the TYPE_NAME column.
     * @return java.lang.String
     */
    public java.lang.String getTypeEnName()
    {
        return this.typeEnName;
    }

    /**
     * Set the value of the TYPE_NAME column.
     * @param typeName
     */
    public void setTypeEnName(java.lang.String typeEnName)
    {
        this.typeEnName = typeEnName;
    } 
    
    /**
     * Implementation of the equals comparison on the basis of equality of the primary key values.
     * @param rhs
     * @return boolean
     */
    public boolean equals(Object rhs)
    {
        if (rhs == null)
            return false;
        if (! (rhs instanceof ConfigEntity))
            return false;
        ConfigEntity that = (ConfigEntity) rhs;
        if (this.getTypeSeri() == null || that.getTypeSeri() == null)
            return false;
        return (this.getTypeSeri().equals(that.getTypeSeri()));
    }

    /**
     * Implementation of the hashCode method conforming to the Bloch pattern with
     * the exception of array properties (these are very unlikely primary key types).
     * @return int
     */
    public int hashCode()
    {
        if (this.hashValue == 0)
        {
            int result = 17;
            int typeSeriValue = this.getTypeSeri() == null ? 0 : this.getTypeSeri().hashCode();
            result = result * 37 + typeSeriValue;
            this.hashValue = result;
        }
        return this.hashValue;
    }
}

具体使用方法
/*Antbee @家军
*/
Config n = new Config();
List View_Stutas = n.Stutas;        request.setAttribute("View_Stutas", View_Stutas);

页面当中示例:
<c:forEach items="${View_Stutas}" var="View_Stutas">
              <option value="<c:out value="${View_Stutas.typeSeri }" />"><c:out value="${View_Stutas.typeEnName}"/></option>
           </c:forEach>

可以看出,在实体类里一共有三个方法,其它有两个名字, 一个是中文名字,一个是英文名字,你可以在页面当中截取本地语言并显示其中一个语言就行。
分享到:
评论
8 楼 BirdGu 2006-09-26  
为解决国际化问题,可以存放字符串资源的键值,而不是直接存放内容。

即使不提供维护界面,放在数据库里也有好处。可以通过数据库工具维护内容。但如果放在程序里,就得改程序了。

当然,放在数据库里也有缺点,就是没有办法利用外键来保证引用完整性了。

完善这个功能是有价值的,因为是很容易做到在多个项目间重用的。
7 楼 LucasLee 2006-09-26  
Not bad。
不过要适应更广泛的应用,这种方法还需要进化。
比如国际化问题,你硬编码了中英文两种语言,再添加其他语言就比较痛苦了。
但是一味完善这类功能,不利于项目的进度。
6 楼 heweiya 2006-09-25  
使用数据库来做呀,其实只需要两张表,以前我也做过类似的设计,叫做数据字典,一张表存所有分类,一张表存所有值。而以上的方法是我在项目当中时切入的,所以如果没有数据库的保存的话,建议使用如上方法。
5 楼 galaxystar 2006-09-25  
干嘛不用Map
4 楼 zhanggok 2006-09-25  
引用

一般我是在数据库里面建立一个叫做MasterData的数据库表,把这些状态信息以一条条记录的形式存在数据库里面,在应用服务器启动的时候,把数据库里面的值Load到内存里面存成一个Map结构。硬编码的方式稍微丑了一点。

为什么要用数据库,难道这些状态有维护的界面?
3 楼 jjljjl1982 2006-09-25  
俺也是!
2 楼 welllove53 2006-09-25  
我们也是用楼上的做法
1 楼 downpour 2006-09-25  
一般我是在数据库里面建立一个叫做MasterData的数据库表,把这些状态信息以一条条记录的形式存在数据库里面,在应用服务器启动的时候,把数据库里面的值Load到内存里面存成一个Map结构。硬编码的方式稍微丑了一点。

相关推荐

    OpenSceneGraph三维渲染引擎设计与实践

    3.1.1 二维与多维向量 32 3.1.2 四元数 35 3.1.3 矩阵 37 3.1.4 包围体 41 3.2 数组对象 44 3.2.1 数据数组 44 3.2.2 数据索引数组 46 3.3 内存管理机制 47 3.3.1 智能指针 48 3.3.2 参照对象 51 3.3.3 ...

    《你必须知道的495个C语言问题》

    1.23 能否声明和传入数组大小一致的局部数组,或者由其他参数指定大小的参数数组? 13 1.24 我在一个文件中定义了一个extern数组,然后在另一个文件中使用,为什么sizeof取不到数组的大小? 13 声明问题 14 ...

    你必须知道的495个C语言问题

    6.18 当我向一个接受指针的指针的函数传入二维数组的时候,编译器报错了。 6.19 我怎样编写接受编译时宽度未知的二维数组的函数? 6.20 我怎样在函数参数传递时混用静态和动态多维数组? 数组的大小 6.21 当...

    Java开发技术大全 电子版

    5.1.6二维数组的使用207 5.1.7for~each循环208 5.1.8应用程序示例1——数组排序209 5.1.9应用程序示例2——杨辉三角212 5.2字符串213 5.2.1String对象的声明213 5.2.2String对象的创建214 5.2.3String对象的...

    Java开发实战1200例(第1卷).(清华出版.李钟尉.陈丹丹).part3

    实例043 将二维数组中的行列互换 53 实例044 利用数组随机抽取幸运观众 54 实例045 用数组设置JTable表格的列名与列宽 55 3.2 数组操作 57 实例046 数组的下标界限 57 实例047 按钮控件数组实现计数器界面 58 实例...

    《程序天下:JavaScript实例自学手册》光盘源码

    14.3 JavaScript创建二维数组 14.4 截断小数点位数 14.5 删除数组中指定元素 14.6 数字选中后放大 14.7 统计字符数的方法 14.8 JavaScript遍历数组 14.9 获取字符串型数组下标的数组长度 14.10 用JavaScript实现数组...

    php网络开发完全手册

    8.1 一维数组与多维数组 119 8.1.1 一维数组简介 119 8.1.2 多维数组简介 119 8.2 常用的数组操作 120 8.2.1 数组的创建与调用 120 8.2.2 数组的更新 121 8.2.3 数组元素的遍历 122 8.3 数组索引与键值的操作技巧 ...

    程序天下:JavaScript实例自学手册

    14.3 JavaScript创建二维数组 14.4 截断小数点位数 14.5 删除数组中指定元素 14.6 数字选中后放大 14.7 统计字符数的方法 14.8 JavaScript遍历数组 14.9 获取字符串型数组下标的数组长度 14.10 用JavaScript实现数组...

    PHP开发实战1200例(第1卷).(清华出版.潘凯华.刘中华).part1

    实例133 二维数组的输出 162 实例134 获取数组当前的键名和值 162 实例135 检测数组中是否存在某个值 163 实例136 获取数组中的当前单元 164 实例137 从数组中随机取出元素 165 实例138 合并数组 166 实例139 拆分...

    PHP开发实战1200例(第1卷).(清华出版.潘凯华.刘中华).part2

    实例133 二维数组的输出 162 实例134 获取数组当前的键名和值 162 实例135 检测数组中是否存在某个值 163 实例136 获取数组中的当前单元 164 实例137 从数组中随机取出元素 165 实例138 合并数组 166 实例139 拆分...

Global site tag (gtag.js) - Google Analytics