设计模式之--代理模式

来源:互联网 发布:手机淘宝买东西流程 编辑:程序博客网 时间:2024/06/05 05:43

代理模式:就是自己不去真正的实现,代用别人的实现,并且来扩充这个被代理的类的功能。

目标:我向创建一个自己的数据库连接池,因此需要增加一个控制的变量,通过该变量我们就可以控制数据库的连接。但是我们自己不想真正的实现Connection接口,因此我们可以使用mysql或者oracle 已经实现的Connection类,进行代理。


如下代码就是代理的MyConnection类的实现:

package daili;


import java.sql.Array;
import java.sql.Blob;
import java.sql.CallableStatement;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.NClob;
import java.sql.PreparedStatement;
import java.sql.SQLClientInfoException;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.SQLXML;
import java.sql.Savepoint;
import java.sql.Statement;
import java.sql.Struct;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.Executor;


public class MyConnection implements Connection{
private Connection conn;
private boolean used=false;

{
try {
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "12345");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
public boolean isUsed() {
return used;
}
public void setUsed(boolean used) {
this.used = used;
}
public void abort(Executor arg0) throws SQLException {
// TODO Auto-generated method stub
conn.abort(arg0);

}
public void clearWarnings() throws SQLException {
// TODO Auto-generated method stub
conn.clearWarnings();

}
public void close() throws SQLException {
// TODO Auto-generated method stub
this.used=true;
}
public void commit() throws SQLException {
// TODO Auto-generated method stub

}
public Array createArrayOf(String arg0, Object[] arg1) throws SQLException {
// TODO Auto-generated method stub
return null;
}
public Blob createBlob() throws SQLException {
// TODO Auto-generated method stub
return null;
}
public Clob createClob() throws SQLException {
// TODO Auto-generated method stub
return null;
}
public NClob createNClob() throws SQLException {
// TODO Auto-generated method stub
return null;
}
public SQLXML createSQLXML() throws SQLException {
// TODO Auto-generated method stub
return null;
}
public Statement createStatement() throws SQLException {
// TODO Auto-generated method stub
return null;
}
public Statement createStatement(int arg0, int arg1) throws SQLException {
// TODO Auto-generated method stub
return null;
}
public Statement createStatement(int arg0, int arg1, int arg2)
throws SQLException {
// TODO Auto-generated method stub
return null;
}
public Struct createStruct(String arg0, Object[] arg1) throws SQLException {
// TODO Auto-generated method stub
return null;
}
public boolean getAutoCommit() throws SQLException {
// TODO Auto-generated method stub
return false;
}
public String getCatalog() throws SQLException {
// TODO Auto-generated method stub
return null;
}
public Properties getClientInfo() throws SQLException {
// TODO Auto-generated method stub
return null;
}
public String getClientInfo(String arg0) throws SQLException {
// TODO Auto-generated method stub
return null;
}
public int getHoldability() throws SQLException {
// TODO Auto-generated method stub
return 0;
}
public DatabaseMetaData getMetaData() throws SQLException {
// TODO Auto-generated method stub
return null;
}
public int getNetworkTimeout() throws SQLException {
// TODO Auto-generated method stub
return 0;
}
public String getSchema() throws SQLException {
// TODO Auto-generated method stub
return null;
}
public int getTransactionIsolation() throws SQLException {
// TODO Auto-generated method stub
return 0;
}
public Map getTypeMap() throws SQLException {
// TODO Auto-generated method stub
return null;
}
public SQLWarning getWarnings() throws SQLException {
// TODO Auto-generated method stub
return null;
}
public boolean isClosed() throws SQLException {
// TODO Auto-generated method stub
return false;
}
public boolean isReadOnly() throws SQLException {
// TODO Auto-generated method stub
return false;
}
public boolean isValid(int arg0) throws SQLException {
// TODO Auto-generated method stub
return false;
}
public String nativeSQL(String arg0) throws SQLException {
// TODO Auto-generated method stub
return null;
}
public CallableStatement prepareCall(String arg0) throws SQLException {
// TODO Auto-generated method stub
return null;
}
public CallableStatement prepareCall(String arg0, int arg1, int arg2)
throws SQLException {
// TODO Auto-generated method stub
return null;
}
public CallableStatement prepareCall(String arg0, int arg1, int arg2,
int arg3) throws SQLException {
// TODO Auto-generated method stub
return null;
}
public PreparedStatement prepareStatement(String arg0) throws SQLException {
// TODO Auto-generated method stub
return null;
}
public PreparedStatement prepareStatement(String arg0, int arg1)
throws SQLException {
// TODO Auto-generated method stub
return null;
}
public PreparedStatement prepareStatement(String arg0, int[] arg1)
throws SQLException {
// TODO Auto-generated method stub
return null;
}
public PreparedStatement prepareStatement(String arg0, String[] arg1)
throws SQLException {
// TODO Auto-generated method stub
return null;
}
public PreparedStatement prepareStatement(String arg0, int arg1, int arg2)
throws SQLException {
// TODO Auto-generated method stub
return null;
}
public PreparedStatement prepareStatement(String arg0, int arg1, int arg2,
int arg3) throws SQLException {
// TODO Auto-generated method stub
return null;
}
public void releaseSavepoint(Savepoint arg0) throws SQLException {
// TODO Auto-generated method stub

}
public void rollback() throws SQLException {
// TODO Auto-generated method stub

}
public void rollback(Savepoint arg0) throws SQLException {
// TODO Auto-generated method stub

}
public void setAutoCommit(boolean arg0) throws SQLException {
// TODO Auto-generated method stub

}
public void setCatalog(String arg0) throws SQLException {
// TODO Auto-generated method stub

}
public void setClientInfo(Properties arg0) throws SQLClientInfoException {
// TODO Auto-generated method stub

}
public void setClientInfo(String arg0, String arg1)
throws SQLClientInfoException {
// TODO Auto-generated method stub

}
public void setHoldability(int arg0) throws SQLException {
// TODO Auto-generated method stub

}
public void setNetworkTimeout(Executor arg0, int arg1) throws SQLException {
// TODO Auto-generated method stub

}
public void setReadOnly(boolean arg0) throws SQLException {
// TODO Auto-generated method stub

}
public Savepoint setSavepoint() throws SQLException {
// TODO Auto-generated method stub
return null;
}
public Savepoint setSavepoint(String arg0) throws SQLException {
// TODO Auto-generated method stub
return null;
}
public void setSchema(String arg0) throws SQLException {
// TODO Auto-generated method stub

}
public void setTransactionIsolation(int arg0) throws SQLException {
// TODO Auto-generated method stub

}
public void setTypeMap(Map arg0) throws SQLException {
// TODO Auto-generated method stub

}
public boolean isWrapperFor(Class arg0) throws SQLException {
// TODO Auto-generated method stub
return false;
}
public Object unwrap(Class arg0) throws SQLException {
// TODO Auto-generated method stub
return null;
}



}


0 0
原创粉丝点击