1. 程式人生 > >java 物件工廠模式讀取配置檔案建立物件

java 物件工廠模式讀取配置檔案建立物件

package com.market.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;

public class ObjectFactory {
	public static HashMap<String, Object> objects = new HashMap<String, Object>();
	static {
		BufferedReader reader = new BufferedReader(new InputStreamReader(
				ObjectFactory.class.getClassLoader().getResourceAsStream(
						"Object")));
		String row = null;
		try {
			while ((row = reader.readLine()) != null) {
				try {
					objects.put(row.split("=")[0], Class.forName(row.split("=")[1]).newInstance());
				} catch (InstantiationException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}