工厂模式
工厂模式开发步骤
1.src 下有个properties 文件
2.编写代码
1.定义一个Map(String,Object)
2.初始化 properties 文件
3.读取配置文件,将其键值对放到map中
4.写一个通过key 获取值的方法
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package test; 2 3 import java.io.BufferedReader; 4 import java.io.InputStream; 5 import java.io.InputStreamReader; 6 import java.util.HashMap; 7 import java.util.Map; 8 9 import org.apache.log4j.Logger; 10 11 public class ObjectFactory { 12 13 private static Logger logger=Logger.getLogger(ObjectFactory.class); 14 15 private static Map<String, Object> objectPool=new HashMap<String,Object>(); 16 static{ 17 InputStream in=ObjectFactory.class.getClassLoader().getResourceAsStream("bean.properties"); 18 BufferedReader bf=new BufferedReader(new InputStreamReader(in)); 19 String entity=null; 20 try { 21 while((entity=bf.readLine())!=null){ 22 String [] str=entity.split("="); 23 objectPool.put(str[0], Class.forName(str[1]).newInstance()); 24 logger.info(""+str[0]+""+str[1]+""); 25 } 26 } catch (Exception e) { 27 e.printStackTrace(); 28 } 29 } 30 31 public static Object getObj(String key){ 32 Object obj=objectPool.get(key); 33 return obj; 34 } 35 36 public static void main(String[] args) { 37 InputStream string=(InputStream) ObjectFactory.getObj("InputStream"); 38 System.out.println(string); 39 } 40 }