隨筆-28  評論-15  文章-81  trackbacks-0
           

          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 譚明 閱讀(246) 評論(0)  編輯  收藏 所屬分類: Java設計模式

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 方城县| 龙泉市| 昭平县| 五指山市| 辉县市| 巨野县| 西丰县| 阿拉善右旗| 南郑县| 乌拉特前旗| 邯郸县| 弋阳县| 荥阳市| 泸西县| 桐梓县| 古蔺县| 勐海县| 炉霍县| 雷山县| 十堰市| 乌拉特后旗| 乌兰浩特市| 永康市| 大庆市| 河间市| 鸡泽县| 张家界市| 台湾省| 上蔡县| 巴里| 石景山区| 邵阳市| 庆城县| 涞源县| 个旧市| 深水埗区| 义乌市| 双牌县| 瓮安县| 广汉市| 贵定县|