在一般的开发过程中,往往要遇到从数据库中取出数据组成值对象(通常叫做VO),然后放在列表集中,并传回调用端的情况。在还没有接触到clone的时候,程序往往要在while块中构造VO实例,但这样做并不推荐,甚至应该极力避免,因为会造成程序效率的低下。在使用clone技术以后,程序运行效率有所改善,以下是常用写法,希望对大家有所帮助。
支持clone的VO看起来像这样:
package com.XXX;
import java.util.*;
public class TempJob implements Cloneable { //声明其可被clone
private String Jbnr=null;
private String Jbdw=null;
public Object clone() {
try {
return super.clone(); //返回clone以后的对象
}
catch (CloneNotSupportedException e) {
System.out.println(e.toString());
}
public void setJbnr(String Jbnr){
this.Jbnr=Jbnr;
}
public String getJbnr(){
return Jbnr;
}
public void setJbdw(String Jbdw){
this.Jbdw=Jbdw;
}
public String getJbdw(){
return Jbdw;
}
}
对于调用VO的类,像这样:
package com.XXX;
import java.sql.*;
import java.util.*;
public class DoJob{
private Connection con;
private Statement stmt;
private ResultSet rs;
private ArrayList lis=new ArrayList();
public ArrayList query(String a){
String sql="";
sql="select a.*,b.name from temp_job a, user_basic b "
+"where a.userid = b.userid order by a.wcqk";
try{
stmt=con.createStatement();
rs=stmt.executeQuery(sql);
TempJob job=new TempJob(); //只要一个实例
while (rs.next()){
job.setJbnr(rs.getString(1));
job.setJbdw(rs.getString(2));
lis.add(job.clone()); //返回被clone对象
}
}
catch(SQLException e){
System.out.println(e.toString());
}
finally {
this.rs=null;
this.stmt=null;
con.close();
}
return lis;
}
}
下面是作者对运行效率的说明:
现在有一典型的 VO 类 Auto(LightWeight): package com.test; public class Auto implements Cloneable{ private String No; private String Addr; public Object clone() throws CloneNotSupportedException{ return super.clone(); } public String setNo(String no){ return this.No=no; } public String getNo(){ return this.No; } public String setAddr(String addr){ return this.Addr=addr; } public String getAddr(){ return this.Addr; } } 接着分别通过使用克隆和不使用克隆的 Bean 来构造 Auto 实例。 使用克隆的 Bean: package com.test; import java.io.*; import java.util.*; import org.dom4j.*; import org.dom4j.io.*; public class MyXMLReader { Auto auto=new Auto(); //请比较不同 ArrayList al=new ArrayList();
public ArrayList go() { long lasting = System.currentTimeMillis(); try { File f = new File("data_100k.xml"); SAXReader reader = new SAXReader(); Document doc = reader.read(f); Element root = doc.getRootElement(); Element foo; for (Iterator i = root.elementIterator("VALUE"); i.hasNext();) { foo = (Element) i.next(); auto.setNo(foo.elementText("NO")); auto.setAddr(foo.elementText("ADDR")); al.add(auto.clone()); //请比较不同 } } catch (Exception e) { e.printStackTrace(); } System.out.println("运行时间:" + (System.currentTimeMillis() - lasting) + " 毫秒"); return al; } } 取八次运行时间 运行时间:172 毫秒 运行时间:93 毫秒 运行时间:94 毫秒 运行时间:141 毫秒 运行时间:125 毫秒 运行时间:78 毫秒 运行时间:203 毫秒 运行时间:63 毫秒 没有使用克隆的 Bean: package com.test; import java.io.*; import java.util.*; import org.dom4j.*; import org.dom4j.io.*; public class MyXMLReader { ArrayList al=new ArrayList(); public ArrayList go() { long lasting = System.currentTimeMillis(); try { File f = new File("data_100k.xml"); SAXReader reader = new SAXReader(); Document doc = reader.read(f); Element root = doc.getRootElement(); Element foo; for (Iterator i = root.elementIterator("VALUE"); i.hasNext();) { foo = (Element) i.next(); Auto auto=new Auto(); //请比较不同 auto.setNo(foo.elementText("NO")); auto.setAddr(foo.elementText("ADDR")); al.add(auto); //请比较不同 } } catch (Exception e) { e.printStackTrace(); } System.out.println("运行时间:" + (System.currentTimeMillis() - lasting) + " 毫秒"); return al; } } 取八次运行时间 运行时间:187 毫秒 运行时间:93 毫秒 运行时间:172 毫秒 运行时间:78 毫秒 运行时间:204 毫秒 运行时间:79 毫秒 运行时间:204 毫秒 运行时间:78 毫秒 通过比较,克隆与非克隆,在构造 Auto 上花的时间是差不多的。 且慢,让我们再看下面的 Auto 类。 修改一下 Auto 类的构造函数,像这样(HeavyWeight): package com.test; import java.io.*; public class Auto implements Cloneable{ private String No; private String Addr; private String str; private File f = new File("data_10k.xml"); private StringBuffer sb=new StringBuffer(); public Object clone() throws CloneNotSupportedException{ return super.clone(); } //以下方法仅仅为了构造一个 HeavyWeight 对象。 public Auto(){ try{ BufferedReader inbuffer=new BufferedReader(new FileReader(f)); while ((str=inbuffer.readLine())!=null){ sb.append(str); } }catch(Exception e){ System.out.println(e.toString()); } } public String setNo(String no){ return this.No=no; } public String getNo(){ return this.No; } public String setAddr(String addr){ return this.Addr=addr; } public String getAddr(){ return this.Addr; } } 再看看测试数据呢? 使用克隆构造 Auto 实例: 不使用克隆构造 Auto 实例: 运行时间:188 毫秒 运行时间:2219 毫秒 运行时间:78 毫秒 运行时间:2266 毫秒 运行时间:109 毫秒 运行时间:2156 毫秒 运行时间:219 毫秒 运行时间:2093 毫秒 运行时间:110 毫秒 运行时间:2266 毫秒 运行时间:78 毫秒 运行时间:2141 毫秒 运行时间:157 毫秒 运行时间:2078 毫秒 运行时间:78 毫秒 运行时间:2172 毫秒 好了,让我们查看一下 Auto 类。可以看见只是在其构造函数中加入读取10K XML文件的代码,
而克隆与非克隆运行时间却有近 10 倍的差距!
↑返回目录
前一篇: java 词汇表速查
后一篇: 用 JAVA 开发游戏连连看