打赏

由歌词引发的模式思考之下篇(模拟Spring的BeanFactory)

  前两篇阐述了FactoryMethodAbstractFactory的实现以及类结构图,但是正如大家所知道的那样,没有任何事情是完美的,这两种设计模式也有自己的优缺点,而Spring对两种工厂模式取长补短,很好的解决了关于生产的产品维度以及产品系列维度问题。

 

三、FactoryMethod与AbstractFactory对比

 FactoryMethod与AbstractFactory对比:

 FactoryMethod主要是生产产品,为产品维度,缺点是生产产品系列时,易导致工厂泛滥。AbstractFactory主要是产品系列,为产品系列维度,缺点是生产产品品种时,工厂改动比较大,不灵活。

四、模拟Spring的BeanFactory的实现

   

1 public interface Moveable {
2     void run();
3 }
1 public class Car implements Moveable{
2     public void run() {
3         System.out.println("car run");
4     }
5 }
1 public class Train implements Moveable{
2     public void run() {
3         System.out.println("train run");
4     }
5 }
1 public interface BeanFactory {
2     Object getBean(String id);
3 }
 1 public class ClassPathXmlApplicationContext implements BeanFactory {
 2     private Map<String,Object> container = new HashMap<String,Object>();
 3     /**
 4      * 采用JDom解析applicationContext.xml
 5      * 具体包可以网上下载
 6      * @param fileName
 7      * @throws JDOMException
 8      * @throws IOException
 9      * @throws InstantiationException
10      * @throws IllegalAccessException
11      * @throws ClassNotFoundException
12      */
13     public ClassPathXmlApplicationContext(String fileName) throws JDOMException, IOException, InstantiationException, IllegalAccessException, ClassNotFoundException {
14         SAXBuilder sb = new SAXBuilder();
15         Document doc = sb.build(this.getClass().getResourceAsStream(fileName));
16         Element root = doc.getRootElement();
17         List list = XPath.selectNodes(root, "/beans/bean");
18 //        System.out.println(list.size());
19         
20         for(int i=0; i<list.size(); i++) {
21             Element bean = (Element) list.get(i);
22             String id = bean.getAttributeValue("id");
23             String clazz = bean.getAttributeValue("class");
24             Object o = Class.forName(clazz).newInstance();
25             container.put(id, o);
26 //            System.out.println(" id: " + id + " class: " + clazz);
27         }
28     }
29     @Override
30     public Object getBean(String id) {
31         return container.get(id);
32     }
33 
34 }

 

1 <!--Spring配置文件applicationContext.xml-->
2 <?xml version="1.0" encoding="UTF-8"?>
3 <beans>
4   <bean id="v" class="com.learn.www.factory.spring.Car">
5   </bean>  
6 </beans>
 1 public class Test {
 2 
 3     /**
 4      * @param args
 5      * @throws IOException 
 6      * @throws ClassNotFoundException 
 7      * @throws IllegalAccessException 
 8      * @throws InstantiationException 
 9      * @throws JDOMException 
10      */
11     public static void main(String[] args) throws IOException, InstantiationException, IllegalAccessException, ClassNotFoundException, JDOMException {
12         BeanFactory f = new ClassPathXmlApplicationContext("applicationContext.xml");
13         Object o = f.getBean("v");
14         Moveable m = (Moveable)o;
15         m.run();
16     }
17 
18 }

 

    所以对Spring给大家一个比较基础的认识就是Spring是一个Bean工厂,一个IOC容器,要求面向接口,面向抽象编程,把具体的东西都配置在配置文件中,以适应灵活的需求变动。

  感谢马老师的学习资料,让我可以深入的学习Java。

posted @ 2013-04-14 23:13  lingjiango  阅读(432)  评论(0编辑  收藏  举报