代理对象Proxy持有对RealSearcher对象、AccessValidator对象和UsageLogger 对象的引用。
系统的时序
如下图所示,此时序图清晰给出了调用发生的顺序:
(1)客户端调用代理对象;
(2)代理对象调用AccessValidator对象,确认用户确实具有相应的权限;
(3)代理对象调用RealSearcher对象,完成查询功能;
(4)代理对象调用UsageLogger对象,完成计数功能;
(5)代理对象将RealSearcher对象的查询结果返回给客户端。
系统源码
1. 客户端代码:Client.java
package com.design.mode.proxy.smartproxy; /** * 客户端源代码 * */ public class Client { //声明一个静态类型为Searcher的静态变量 private static Searcher searcher; public static void main(String[] args) { searcher = new Proxy(); String userId ="000001"; try{ String result = searcher.doSearch(userId); System.out.println("============"+result); }catch(Exception ex){ ex.printStackTrace(); } } }2.抽象主题代码:Searcher.java
package com.design.mode.proxy.smartproxy; /** * 抽象主题角色 * */ public interface Searcher { //声明一个抽象方法 public String doSearch(String userId) throws Exception; }3.代理角色代码:Proxy.java
package com.design.mode.proxy.smartproxy; /** * 代理角色源代码 * */ public class Proxy implements Searcher{ private RealSearcher searcher; private AccessValidator accessValidator; private UsageLogger usageLogger; /** * 构造子 * */ public Proxy(){ searcher = new RealSearcher(); } //查询前的权限操作 private boolean checkAccess(String userId){ accessValidator = new AccessValidator(); boolean bo= accessValidator.vaidate(userId); return bo; } //查询后,将记录写入日志 private void logUsage(String userId){ usageLogger =new UsageLogger(); usageLogger.setUserId(userId); usageLogger.save(); } public String doSearch(String userId) throws Exception { if(checkAccess(userId)){ String result = searcher.doSearch(userId); logUsage(userId); return result; } return null; } }4.真实主题代码:RealSearcher.java
package com.design.mode.proxy.smartproxy; /** * 真实主题角色 * */ import com.design.mode.proxy.dao.DbJavabean; public class RealSearcher implements Searcher { //构造函数 public RealSearcher(){ } public String doSearch(String userId) throws Exception { DbJavabean db = new DbJavabean(); String str=db.selUserMsg(userId); return str; } }5.检查权限的对象的代码:AccessValidator.java
package com.design.mode.proxy.smartproxy; import com.design.mode.proxy.dao.DbJavabean; /** * 检查权限 * */ public class AccessValidator { public boolean vaidate(String userId){ DbJavabean db = new DbJavabean(); String userType = db.selUserType(userId); if(userType.equals("admin")){ return true; }else return false; } }6.记录次数的代码:UsageLogger.java
package com.design.mode.proxy.smartproxy; import com.design.mode.proxy.dao.DbJavabean; /** * 记录次数代码 * */ public class UsageLogger { private String userId; /** * 用户的赋值方法 * */ public void setUserId(String userId){ this.userId = userId; } public void save(){ DbJavabean db = new DbJavabean(); db.upCount(userId); } }7.数据库连接的类 DbJavabean.java
package com.design.mode.proxy.dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; public class DbJavabean { public String conndb(String sql,int x) throws Exception{ Connection conn = null; String userType =""; try{ Class.forName("oracle.jdbc.driver.OracleDriver"); String url ="jdbc:oracle:thin:@10.10.9.110:1521:infonet"; String user="infonet"; String password ="infonet2"; conn = DriverManager.getConnection(url, user, password); //Statement st = conn.createStatement(); //PreparedStatement 接口继承Statement PreparedStatement对象已预编译过效率要快于Statement PreparedStatement st = conn.prepareStatement(sql); if(x==0){ //st.executeQuery(sql); st.executeQuery(); ResultSet rs = st.getResultSet(); while(rs.next()){ userType = rs.getString("userMsg"); } }else if(x==1){ st.executeQuery(sql); ResultSet rs = st.getResultSet(); while(rs.next()){ userType = rs.getString("userType"); } }else{ userType = st.executeUpdate(sql)+ ""; } conn.commit(); st.close(); conn.close(); }catch(Exception ex){ ex.printStackTrace(); throw ex; }finally{ conn.close(); } return userType; } //查询用户状态 public String selUserType(String userId){ int x = 1; String userType=""; String sql ="select usertype from proxy_demo_user pu where pu.userId ="+userId; try{ userType=conndb(sql, x); }catch(Exception ex){ ex.printStackTrace(); } return userType; } //查询用户信息 public String selUserMsg(String userId){ int x =0; String userMsg=""; String sql ="select usermsg from proxy_demo_user pu where pu.userId = "+userId; try{ userMsg=conndb(sql, x); }catch(Exception ex){ ex.printStackTrace(); } return userMsg; } //记录用户次数 public void upCount(String userId){ int x = 2; String sql ="update proxy_demo_user pu set pu.usercount = pu.usercount +1 where pu.userId ="+userId; try{ conndb(sql, x); }catch(Exception ex){ ex.printStackTrace(); } } }