首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

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

2012-10-29 
用二维数组管理好你零乱的状态、分类和其它常用选项在大家的编码过程当中,有没有遇到过这么一种情况,很多零

用二维数组管理好你零乱的状态、分类和其它常用选项
在大家的编码过程当中,有没有遇到过这么一种情况,很多零乱的状态、分类和其它常用选项常常是定义死了。但是没有一个完整的东东来约束他,在每个模块当中使用相关的信息时,往往重新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>

可以看出,在实体类里一共有三个方法,其它有两个名字, 一个是中文名字,一个是英文名字,你可以在页面当中截取本地语言并显示其中一个语言就行。 1 楼 downpour 2006-09-25   一般我是在数据库里面建立一个叫做MasterData的数据库表,把这些状态信息以一条条记录的形式存在数据库里面,在应用服务器启动的时候,把数据库里面的值Load到内存里面存成一个Map结构。硬编码的方式稍微丑了一点。 2 楼 welllove53 2006-09-25   我们也是用楼上的做法 3 楼 jjljjl1982 2006-09-25   俺也是! 4 楼 zhanggok 2006-09-25   引用
一般我是在数据库里面建立一个叫做MasterData的数据库表,把这些状态信息以一条条记录的形式存在数据库里面,在应用服务器启动的时候,把数据库里面的值Load到内存里面存成一个Map结构。硬编码的方式稍微丑了一点。

为什么要用数据库,难道这些状态有维护的界面?
5 楼 galaxystar 2006-09-25   干嘛不用Map 6 楼 heweiya 2006-09-25   使用数据库来做呀,其实只需要两张表,以前我也做过类似的设计,叫做数据字典,一张表存所有分类,一张表存所有值。而以上的方法是我在项目当中时切入的,所以如果没有数据库的保存的话,建议使用如上方法。 7 楼 LucasLee 2006-09-26   Not bad。
不过要适应更广泛的应用,这种方法还需要进化。
比如国际化问题,你硬编码了中英文两种语言,再添加其他语言就比较痛苦了。
但是一味完善这类功能,不利于项目的进度。
8 楼 BirdGu 2006-09-26   为解决国际化问题,可以存放字符串资源的键值,而不是直接存放内容。

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

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

完善这个功能是有价值的,因为是很容易做到在多个项目间重用的。

热点排行