當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
由歌词引发的模式思考之下篇(模拟Spring的BeanFactory)
生活随笔
收集整理的這篇文章主要介紹了
由歌词引发的模式思考之下篇(模拟Spring的BeanFactory)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
前兩篇闡述了FactoryMethod和AbstractFactory的實現以及類結構圖,但是正如大家所知道的那樣,沒有任何事情是完美的,這兩種設計模式也有自己的優缺點,而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。
轉載于:https://www.cnblogs.com/iou123lg/archive/2013/04/14/3021229.html
總結
以上是生活随笔為你收集整理的由歌词引发的模式思考之下篇(模拟Spring的BeanFactory)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: hdu1010深搜+奇偶剪枝
- 下一篇: JavaScript那些事儿(1):对比