package com.tanm.factoryDemo;
interface Car {
    public void start();
    public void stop();
}
class Benz implements Car {
    public void start() {
       System.out.println("Benz开动了。。。。");
    }
    public void stop() {
       System.out.println("Benz停车了。。。");
    }
}
class Ford implements Car {
    public void start() {
       System.out.println("Ford开动了。。。。");
    }
    public void stop() {
       System.out.println("Ford停车了。。。");
    }
}
class BigBus implements Car {
    public void start() {
       System.out.println("大巴开车了。。。。");
    }
    public void stop() {
       System.out.println("大巴停车了。。。。");
    }
}
class MiniBus implements Car {
    public void start() {
       System.out.println("小巴开车了。。。。");
    }
    public void stop() {
       System.out.println("小巴停车了。。。。");
    }
}
//抽象工厂
interface AbstractFactory {
}
//具体小汽车工厂
class CarFactory implements AbstractFactory {
    public Car getCar(String type) {
       Car c = null;
       try {
           c = (Car) Class.forName("org.jzkangta.factorydemo02." + type)
                  .newInstance();
       } catch (InstantiationException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       } catch (IllegalAccessException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       } catch (ClassNotFoundException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       }
       return c;
    }
}
//具体公共汽车工厂
class BusFactory implements AbstractFactory {
    public Car getBus(String type) {
       Car c = null;
       try {
           c = (Car) Class.forName("org.jzkangta.factorydemo02." + type)
                  .newInstance();
       } catch (InstantiationException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       } catch (IllegalAccessException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       } catch (ClassNotFoundException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       }
       return c;
    }
}
public class FactoryDemo {
    public static void main(String[] args) {
       // CarFactory cf=new CarFactory();
       BusFactory bf = new BusFactory();
       Car c = null;
       // c=cf.getCar("Benz");
       c = bf.getBus("BigBus");
       c.start();
       c.stop();
    }
}
	posted on 2007-10-16 18:09 
谭明 阅读(257) 
评论(0)  编辑  收藏  所属分类: 
Java设计模式