工厂模式是你们很是熟悉的设计模式,spring BeanFactory将此模式发扬光大,今天就来说讲,spring IOC容器是如何帮我管理Bean,构造对象的,简单的写了一个demo。java
思路以下:spring
关于什么是工厂模式以前写过一个:http://www.javashuo.com/article/p-fbilqccu-ek.html设计模式
package com.spring.xml; public interface Pizza{ public float getPrice(); }
package com.spring.xml; public class MargheritaPizza implements Pizza{ public float getPrice() { System.out.println("8.5f"); return 8.5f; } }
public class CalzonePizza implements Pizza{ public float getPrice() { System.out.println("2.5f"); return 2.5f; } }
经过传入参数id,选择不一样的实例类,若是后续不断的增长新类,会频繁的修改create方法,不符合开闭原则app
public class PizzaFactory { public Pizza create(String id) { if (id == null) { throw new IllegalArgumentException("id is null!"); } if ("Calzone".equals(id)) { return new CalzonePizza(); } if ("Margherita".equals(id)) { return new MargheritaPizza(); } throw new IllegalArgumentException("Unknown id = " + id); } }
package com.spring.xml; 2 3 public interface BeanFactory { 4 Object getBean(String id); 5 }
BeanFactory实现类,主要功能是解析xml文件,封装bean到map中dom
public class ClassPathXmlApplicationContext implements BeanFactory { private Map<String, Object> beans = new HashMap<String, Object>(); public ClassPathXmlApplicationContext(String fileName) throws Exception{ SAXReader reader = new SAXReader(); Document document = reader.read(this.getClass().getClassLoader().getResourceAsStream(fileName)); List<Element> elements = document.selectNodes("/beans/bean"); for (Element e : elements) { //获取beanId String id = e.attributeValue("id"); //获取beanId对于的ClassPath下class全路径名称 String value = e.attributeValue("class"); //经过反射实例化该beanId 的class对象 Object o = Class.forName(value).newInstance(); //封装到一个Map里 beans.put(id, o); } } public Object getBean(String id) { return beans.get(id); } }
<?xml version="1.0" encoding="UTF-8"?> <beans> <bean id="calzone" class="com.spring.xml.CalzonePizza"></bean> <bean id="margherita" class="com.spring.xml.MargheritaPizza"></bean> </beans>
写一个类测试一下测试
package com.spring.xml; import org.dom4j.DocumentException; public class Test { public static void main(String[] args) throws Exception { BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml"); Pizza p = (Pizza)factory.getBean("Calzone"); p.getPrice(); } }
看完代码应该很是清楚,比起原先经过if else来选择不一样的实现类方便。this