<pre name="code" class="java">import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
public class BasicFactory {
public static Properties prop= null;
private static BasicFactory factory = new BasicFactory();
private BasicFactory(){}
static{
try {
prop = new Properties();
prop.load(new FileReader(BasicFactory.class.getClassLoader().getResource("config.properties").getPath()));
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
public static BasicFactory getFactory(){
return factory;
}
public <T> T getInstance(Class<T> clazz){
try {
String simpleName = clazz.getSimpleName();
String path= prop.getProperty(simpleName);
return (T) Class.forName(path).newInstance();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
工厂模式
最新推荐文章于 2015-08-10 15:14:10 发布