关于static {getInstance();}的问题
public class DaoObjectFactory {
private static Object initLock = new Object();
private static String[] classNames = {"org.glgk.job.dao.DaoArticle",
"org.glgk.job.dao.DaoBaseInfo",
"org.glgk.job.dao.DaoCorporation",
"org.glgk.job.dao.DaoEducation",
"org.glgk.job.dao.DaoExperience",
"org.glgk.job.dao.DaoInterview",
"org.glgk.job.dao.DaoIpBlock",
"org.glgk.job.dao.DaoJobPost",
"org.glgk.job.dao.DaoJobSearch",
"org.glgk.job.dao.DaoManage",
"org.glgk.job.dao.DaoPersonal",
"org.glgk.job.dao.DaoTypes",
"org.glgk.job.dao.DaoUser",
"org.glgk.job.dao.DaoFriendLink",
"org.glgk.job.dao.DaoAdvertising",
"org.glgk.job.dao.DaoJobApply",
"org.glgk.job.dao.DaoArticleType",
"org.glgk.job.dao.DaoJobFavorite",
"org.glgk.job.dao.DaoPersonalSecret",
"org.glgk.job.dao.DaoIntent",
"org.glgk.job.dao.DaoAnnounce",
"org.glgk.job.dao.DaoAgentProps",
"org.glgk.job.dao.DaoRequestPost"
};
private static Object[] objects = null;
static{
getInstance();
}
public static DaoRequestPost getDaoRequestPost() {
return (DaoRequestPost) objects[22];
}
public static DaoIntent getDaoIntent() {
return (DaoIntent) objects[19];
}
public static DaoAnnounce getDaoAnnounce() {
return (DaoAnnounce) objects[20];
}
public static DaoAgentProps getDaoAgentProps() {
return (DaoAgentProps) objects[21];
}
private static Object[] getInstance(){
if (objects == null) {
synchronized (initLock) {
try {
objects = new Object[classNames.length];
for (int i=0; i<classNames.length; i++){
Class<?> c = Class.forName(classNames[i]);
objects[i] = c.newInstance();
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
return objects;
}
上面的代码中,static {getInstance();}是被默认最先执行的么,就好像类的构造器一样?外部调用该类的一个方法:
DaoAnnounce annoucne= DaoObjectFactory.getDaoAnnounce()就可以直接获得类DaoAnnounce的一个对象句柄。
我想问:在DaoObjectFactory类中,getInstance()方法是负责构造string[] Classname 数组里的每个对象么?
------解决方案--------------------static代码块在类加载的时候就运行,这个程序里面用来初始化objects,使其内每一个对象都对应classNames里的每一个类名
顺便问一下,我对设计模式不很了解,这个程序就是单例模式和工厂模式吧?