public String getName();
public void setName(String name);
public String getDepId();
public void setDepId(String depId);
public String getSex();
public void setSex(String sex);
}
(2)定义了接口,需要让UserModel来实现它。基本没有什么变化,只是要实现这个新的接口而已,就不去代码示例了。
(3)接下来看看新加入的代理对象的实现,示例代码如下:
| /** * 代理对象,代理用户数据对象 */ public class Proxy implements UserModelApi{ /** * 持有被代理的具体的目标对象 */ private UserModel realSubject=null;
/** * 构造方法,传入被代理的具体的目标对象 * @param realSubject 被代理的具体的目标对象 */ public Proxy(UserModel realSubject){ this.realSubject = realSubject; } /** * 标示是否已经重新装载过数据了 */ private boolean loaded = false;
public String getUserId() { return realSubject.getUserId(); } public void setUserId(String userId) { realSubject.setUserId(userId); } public String getName() { return realSubject.getName(); } public void setName(String name) { realSubject.setName(name); } public void setDepId(String depId) { realSubject.setDepId(depId); } public void setSex(String sex) { realSubject.setSex(sex); } public String getDepId() { //需要判断是否已经装载过了 if(!this.loaded){ //从数据库中重新装载 reload(); //设置重新装载的标志为true this.loaded = true; } return realSubject.getDepId(); } public String getSex() { if(!this.loaded){ reload(); this.loaded = true; } return realSubject.getSex(); } /** * 重新查询数据库以获取完整的用户数据 */ private void reload(){ System.out.println("重新查询数据库获取完整的用户数据,userId==" +realSubject.getUserId()); Connection conn = null; try{ conn = this.getConnection(); String sql = "select * from tbl_user where userId= ";
PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setString(1, realSubject.getUserId()); ResultSet rs = pstmt.executeQuery(); if(rs.next()){ //只需要重新获取除了userId和name外的数据 realSubject.setDepId(rs.getString("depId")); realSubject.setSex(rs.getString("sex")); }
rs.close(); pstmt.close(); }catch(Exception err){ err.printStackTrace(); }finally{ try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } public String toString(){ return "userId="+getUserId()+",name="+getName() +",depId="+getDepId()+",sex="+getSex()+"\n"; } private Connection getConnection() throws Exception { Class.forName("你用的数据库对应的JDBC驱动类"); return DriverManager.getConnection( "连接数据库的URL", "用户名", "密码"); } } |
(3)看看此时UserManager的变化,大致如下:
- 从数据库查询值的时候,不需要全部获取了,只需要查询用户编号和姓名的数据就可以了
- 把数据库中获取的值转变成对象的时候,创建的对象不再是UserModel,而是代理对象,而且设置值的时候,也不是全部都设置,只是设置用户编号和姓名两个属性的值
示例代码如下:
/**
* 实现示例要求的功能
*/
public class UserManager {
/**
* 根据部门编号来获取该部门下的所有人员
* @param depId 部门编号
* @return 该部门下的所有人员
*/
public Collection
getUserByDepId( String depId)throws Exception{
Collection
col = new ArrayList
(); Connection conn = null;
try{
conn = this.getConnection();
//只需要查询userId和name两个值就可以了
String sql = "select u.userId,u.name "
+"from tbl_user u,tbl_dep d "
+"where u.depId=d.depId and d.depId like ";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, depId+"%");
ResultSet rs = pstmt.executeQuery();
while(rs.next()){
//这里是创建的代理对象,而不是直接创建UserModel的对象
Proxy proxy = new Proxy(new UserModel());
//只是设置userId和name两个值就可以了
proxy.setUserId(rs.getString("userId"));
proxy.setName(rs.getString("name"));
col.add(proxy);
}
rs.close();
pstmt.close();
}finally{
conn.close();
}
return col;
}
private Connection getConnection() throws Exception {
Class.forName("你用的数据库对应的JDBC驱动类");
return DriverManager.getConnection(
"连接