隨筆-28  評(píng)論-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開(kāi)動(dòng)了。。。。");

              }

              public void stop() {

                 System.out.println("Benz停車了。。。");

              }

          }

          class Ford implements Car {

              public void start() {

                 System.out.println("Ford開(kāi)動(dòng)了。。。。");

              }

              public void stop() {

                 System.out.println("Ford停車了。。。");

              }

          }

          class BigBus implements Car {

              public void start() {

                 System.out.println("大巴開(kāi)車了。。。。");

              }

              public void stop() {

                 System.out.println("大巴停車了。。。。");

              }

          }

          class MiniBus implements Car {

              public void start() {

                 System.out.println("小巴開(kāi)車了。。。。");

              }

              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) 評(píng)論(0)  編輯  收藏 所屬分類: Java設(shè)計(jì)模式

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 红桥区| 如东县| 余江县| 高唐县| 崇文区| 夏邑县| 邮箱| 滨州市| 中江县| 酉阳| 新余市| 南丰县| 三亚市| 南岸区| 中山市| 竹山县| 宾阳县| 固镇县| 阳原县| 永胜县| 岐山县| 高唐县| 奎屯市| 濮阳县| 永年县| 大田县| 安岳县| 五寨县| 阳山县| 达尔| 全椒县| 镇雄县| 张掖市| 育儿| 多伦县| 新沂市| 肇东市| 萍乡市| 兴山县| 封开县| 武冈市|