javascript
由歌词引发的模式思考之下篇(模拟Spring的BeanFactory)
前兩篇闡述了FactoryMethod和AbstractFactory的實(shí)現(xiàn)以及類結(jié)構(gòu)圖,但是正如大家所知道的那樣,沒有任何事情是完美的,這兩種設(shè)計(jì)模式也有自己的優(yōu)缺點(diǎn),而Spring對(duì)兩種工廠模式取長(zhǎng)補(bǔ)短,很好的解決了關(guān)于生產(chǎn)的產(chǎn)品維度以及產(chǎn)品系列維度問題。
?
三、FactoryMethod與AbstractFactory對(duì)比
FactoryMethod與AbstractFactory對(duì)比:
FactoryMethod主要是生產(chǎn)產(chǎn)品,為產(chǎn)品維度,缺點(diǎn)是生產(chǎn)產(chǎn)品系列時(shí),易導(dǎo)致工廠泛濫。AbstractFactory主要是產(chǎn)品系列,為產(chǎn)品系列維度,缺點(diǎn)是生產(chǎn)產(chǎn)品品種時(shí),工廠改動(dòng)比較大,不靈活。
四、模擬Spring的BeanFactory的實(shí)現(xiàn)
???
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 * 具體包可以網(wǎng)上下載 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 }?
??? 所以對(duì)Spring給大家一個(gè)比較基礎(chǔ)的認(rèn)識(shí)就是Spring是一個(gè)Bean工廠,一個(gè)IOC容器,要求面向接口,面向抽象編程,把具體的東西都配置在配置文件中,以適應(yīng)靈活的需求變動(dòng)。
感謝馬老師的學(xué)習(xí)資料,讓我可以深入的學(xué)習(xí)Java。
轉(zhuǎn)載于:https://www.cnblogs.com/iou123lg/archive/2013/04/14/3021229.html
總結(jié)
以上是生活随笔為你收集整理的由歌词引发的模式思考之下篇(模拟Spring的BeanFactory)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: hdu1010深搜+奇偶剪枝
- 下一篇: JavaScript那些事儿(1):对比