package net.util;/** * @項目名:spring2.5 * @包名:net.util * @檔案名稱:FactoryDemo.java * @日期:Jun 21, 2011 4:37:02 PM * @備忘:原廠模式 * @作者:apple */public class FactoryDemo {/*** @param args*/public static void main(String[] args) {// TODO Auto-generated method stubCar c=Factory.getCarInstance("Banz");if(c!=null){c.run();c.stop();}else{System.out.println("製造不了");}}}class Factory{public static Car getCarInstance(String type){Car c=null;try {c=(Car)Class.forName("net.util."+type).newInstance();} catch (InstantiationException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalAccessException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}return c;}}interface Car{public void run();public void stop();}class Banz implements Car{public void run() {// TODO Auto-generated method stubSystem.out.println("Banz跑");}public void stop() {// TODO Auto-generated method stubSystem.out.println("Banz停");}}class Ford implements Car{public void run() {// TODO Auto-generated method stubSystem.out.println("Ford跑");}public void stop() {// TODO Auto-generated method stubSystem.out.println("Ford停");}}