您的位置:首页 > 其它

利用工厂模式实现Dao层和Service的解耦

2015-08-14 20:15 288 查看
dao.properties的文件内容

UpfileDao=cn.itcast.dao.impl.UpfileDaoImpl


DaoFactory的实现

public class DaoFactory {

private static Properties daoconfig = new Properties();
static{
try {
daoconfig.load(DaoFactory.class.getClassLoader().getResourceAsStream("dao.properties"));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private DaoFactory(){}
private static final DaoFactory instance = new DaoFactory();
public static DaoFactory getInstance(){
return instance;
}

public <T> T createDao(Class<T> interfaceClass){
String name = interfaceClass.getSimpleName();
String daoClassname = daoconfig.getProperty(name);
try {
return (T) Class.forName(daoClassname).newInstance();
} catch (Exception e) {
throw new RuntimeException(e);
}
}

}


BusinessServiceImpl利用DaoFactory进行解耦

private UpfileDao dao = DaoFactory.getInstance().createDao(UpfileDao.class);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: