首页 | 中文黄页 | 图片共享

渥太华华人网上社区
注册账号 Blog 论坛帮助 会员列表 搜索 今日新帖 标记版面已读 用户相册
返回   CFC加拿大中文论坛 > 科技时代 > 电脑技术 > 有没有搞过Class.forName的,求助
回复
 
主题工具 显示模式
[渥村标题党党代表]
ID: 3308
变态 热狗 的头像
变态 热狗 即将成为新星变态 热狗 即将成为新星变态 热狗 即将成为新星变态 热狗 即将成为新星变态 热狗 即将成为新星变态 热狗 即将成为新星变态 热狗 即将成为新星变态 热狗 即将成为新星变态 热狗 即将成为新星变态 热狗 即将成为新星变态 热狗 即将成为新星
声望: 83461 声望力: 914
住址: Ottawa
帖子: 26,531
家族: 北京中加
来自: 福清
文章: 72
金钱: 62,845
注册: 2002-10-12
有没有搞过Class.forName的,求助

www.comefromchina.com
最近闲来无事,所以想研究以下dynamic class loading
例如我要实现一个generic database client.
支持mysql/postgresql, 在用户安装程序的时候选择使用mysql或者postgresql
private static final String[]dbtypes = {"test.db.servers.MySQL.MySQL","test.db.servers.PostgreSQL.PostgreSQL"};
private static final String[]qyerttypes = {"test.db.servers.MySQL.QueryObject","test.db.servers.PostgreSQL.QueryObject"};
private IDatabase db;
private IQueryObject query;
private Class DBClass;
private Class QueryClass;

try{
dbclass = Class.forName(dbtypes[1]);
db = (IDatabase) dbclass.newInstance();
querylass = Class.forName(querytypes[1]);
query = (IQueryObject) QueryClass.netInstance();
}catch(.....)....

问题是performance是不是会比直接建立对应的sql lib更慢,会慢多少呢?
________________________________
YOU CAN'T ALWAYS GET WHAT YOU WANT
la familia es todo | c'est la vie
变态 热狗 当前离线  
回复时引用此帖
旧 2008-07-08, 09:01 #1
[渥村标题党党代表]
ID: 3308
变态 热狗 的头像
变态 热狗 即将成为新星变态 热狗 即将成为新星变态 热狗 即将成为新星变态 热狗 即将成为新星变态 热狗 即将成为新星变态 热狗 即将成为新星变态 热狗 即将成为新星变态 热狗 即将成为新星变态 热狗 即将成为新星变态 热狗 即将成为新星变态 热狗 即将成为新星
声望: 83461 声望力: 914
住址: Ottawa
帖子: 26,531
家族: 北京中加
来自: 福清
文章: 72
金钱: 62,845
注册: 2002-10-12

Based on my understanding, that we could use singleton pattern to create the QueryObject in PostgreSQL package.
@Override
public IQueryObject getInstance(String obj) {
if (instance ==null) instance = new QueryObject();
this.obj = obj;
return instance;
}
However as a database, there is no way it only performs single task, which means we also need to take synchronization into account. That means we might need to create a pool of IQueryObject, use a getNextQueryObject to create a query. I wonder if i am on the right track, lol? Feel free to drop any comments

此帖于 2008-07-08 10:33 被 变态 热狗 编辑.
变态 热狗 当前离线  
回复时引用此帖
旧 2008-07-08, 09:13 #2
[渥村标题党党代表]
ID: 3308
变态 热狗 的头像
变态 热狗 即将成为新星变态 热狗 即将成为新星变态 热狗 即将成为新星变态 热狗 即将成为新星变态 热狗 即将成为新星变态 热狗 即将成为新星变态 热狗 即将成为新星变态 热狗 即将成为新星变态 热狗 即将成为新星变态 热狗 即将成为新星变态 热狗 即将成为新星
声望: 83461 声望力: 914
住址: Ottawa
帖子: 26,531
家族: 北京中加
来自: 福清
文章: 72
金钱: 62,845
注册: 2002-10-12

我回来自问自答了

package test.database;

import java.lang.reflect.Constructor;
import test.database.interfaces.IDatabase;
import test.database.interfaces.IQueryObject;

public class Database {
private static final int DATABASEINDEX = 1;
private static final String[]
DBTypes = { "test.database.servers.MySQL.MySQL",
"test.database.servers.PostgreSQL.PostgreSQL"
};
private static final String[]
QueryTypes = { "test.database.servers.MySQL.QueryObject",
"test.database.servers.PostgreSQL.QueryObject"
};

private IDatabase database;

public void init()
{
try
{
database = (IDatabase)Class.forName( DBTypes[ DATABASEINDEX ] ).newInstance();
}
catch( Exception e )
{
System.out.println( "Exception occurred: " + e.getMessage() );
e.printStackTrace();
}
}
public IQueryObject getQueryObject(String QueryOperation){

Constructor constr;
IQueryObject ret = null;

try {
constr = Class.forName( QueryTypes[ DATABASEINDEX ] ).getConstructor( String.class );
ret = (IQueryObject)constr.newInstance( QueryOperation );
//return (IQueryObject)Class.forName( QueryTypes[ 1 ] ).getConstructor(String.class).newInstance(start);
} catch (Exception e) {
e.printStackTrace();
}
return ret;
}
public void getSQL()
{
IQueryObject select = null,select2 = null;
database.initialize();
database.connect("localhost", 5432, "foo", "sqlogin", "");

select = getQueryObject(QueryOperations.SELECT);
select.set_select_list("*");
select.set_source("table1");
select.set_limit(1);

select2 = getQueryObject(QueryOperations.SELECT);
select2.set_select_list("*");
select2.set_source("table2");
select2.set_limit(1);

database.select(select);
database.select(select2);

database.close();

select.clear();
select2.clear();
select = null;
select2 = null;
}
public static void main(String[] argv) {
Database db = new Database();
db.init();
db.getSQL();
}

}
这个可以了
________________________________
YOU CAN'T ALWAYS GET WHAT YOU WANT
la familia es todo | c'est la vie
变态 热狗 当前离线  
回复时引用此帖
旧 2008-07-08, 12:31 #3
[渥村标题党党代表]
ID: 3308
变态 热狗 的头像
变态 热狗 即将成为新星变态 热狗 即将成为新星变态 热狗 即将成为新星变态 热狗 即将成为新星变态 热狗 即将成为新星变态 热狗 即将成为新星变态 热狗 即将成为新星变态 热狗 即将成为新星变态 热狗 即将成为新星变态 热狗 即将成为新星变态 热狗 即将成为新星
声望: 83461 声望力: 914
住址: Ottawa
帖子: 26,531
家族: 北京中加
来自: 福清
文章: 72
金钱: 62,845
注册: 2002-10-12

just realized that delegation in java is doable.

Class aClass = ...;
Object parameters[] = {String.class, int.class};
Method method = aClass.getMethod("method", parameters);
//call method(String, int)
Object arguments[] = {"Hello", new Integer(3)};
returnValue = method.invoke(aClassInstance, arguments);
________________________________
YOU CAN'T ALWAYS GET WHAT YOU WANT
la familia es todo | c'est la vie
变态 热狗 当前离线  
回复时引用此帖
旧 2008-07-08, 16:29 #4
[黄金长老]
ID: 36983
雍小锋 的头像
雍小锋 身上有一圈迷人的光环哦雍小锋 身上有一圈迷人的光环哦雍小锋 身上有一圈迷人的光环哦雍小锋 身上有一圈迷人的光环哦雍小锋 身上有一圈迷人的光环哦雍小锋 身上有一圈迷人的光环哦雍小锋 身上有一圈迷人的光环哦雍小锋 身上有一圈迷人的光环哦雍小锋 身上有一圈迷人的光环哦雍小锋 身上有一圈迷人的光环哦雍小锋 身上有一圈迷人的光环哦
声望: 39025 声望力: 442
帖子: 2,280
家族: 北京中加
来自:
金钱: 15,554
注册: 2004-09-04

牛逼
雍小锋 当前离线  
回复时引用此帖
旧 2008-07-08, 21:11 #5
[渥村标题党党代表]
ID: 3308
变态 热狗 的头像
变态 热狗 即将成为新星变态 热狗 即将成为新星变态 热狗 即将成为新星变态 热狗 即将成为新星变态 热狗 即将成为新星变态 热狗 即将成为新星变态 热狗 即将成为新星变态 热狗 即将成为新星变态 热狗 即将成为新星变态 热狗 即将成为新星变态 热狗 即将成为新星
声望: 83461 声望力: 914
住址: Ottawa
帖子: 26,531
家族: 北京中加
来自: 福清
文章: 72
金钱: 62,845
注册: 2002-10-12

............
给点建设性的回复好吗?
________________________________
YOU CAN'T ALWAYS GET WHAT YOU WANT
la familia es todo | c'est la vie
变态 热狗 当前离线  
回复时引用此帖
旧 2008-07-08, 21:27 #6
回复


主题工具
显示模式




所有时间均为格林尼治时间 -5。现在的时间是 10:31

论坛内容均为网友自由上传, 不代表本网观点。
comefromchina.com does not represent or guarantee the truthfulness, accuracy, or reliability of any of communications posted by other users.


Powered by vBulletin® 版本 3.6.8
版权所有 ©2000 - 2008,Jelsoft Enterprises Ltd.
Copyright @ comefromchina.com 2007

SEO by vBSEO 3.1.0 ©2007, Crawlability, Inc.