Java工厂模式创建对象


public class DaoFactory {
    private static Properties pro = new Properties();
    static {
        try {
            pro.load(new FileReader("dao.txt"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static Object getBean(String interfaceName) {
        //根据键,得到值
        String value = pro.getProperty(interfaceName);
        try {
            //加载类  得到类模板
            Class c = Class.forName(value);
            //调用无参构造方法,产生实现类对象
            return c.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

 

 

 

posted @ 2020-08-10 09:54  FivePointOne  阅读(661)  评论(0编辑  收藏  举报