隨筆 - 117  文章 - 72  trackbacks - 0

          聲明:原創(chuàng)作品(標(biāo)有[原]字樣)轉(zhuǎn)載時(shí)請(qǐng)注明出處,謝謝。

          常用鏈接

          常用設(shè)置
          常用軟件
          常用命令
           

          訂閱

          訂閱

          留言簿(7)

          隨筆分類(130)

          隨筆檔案(123)

          搜索

          •  

          積分與排名

          • 積分 - 156206
          • 排名 - 390

          最新評(píng)論

          [關(guān)鍵字]:java,設(shè)計(jì)模式,簡(jiǎn)單工廠,《java與模式》
          [環(huán)境]:StarUML + JDK6
          [作者]:Winty (wintys@gmail.com)
              簡(jiǎn)單工廠模式:

              代碼:
          /**
           *簡(jiǎn)單工廠測(cè)試
           *@version 2009-3-25
           *@author Winty(wintys@gmail.com)
           */
          package pattern.simplefactory;

          public class SimpleFactory{
              public static void main(String[] args){
                  Shape shape;

                  try{
                      shape = ArtTracer.factory("circle");
                      shape.draw();
                      shape.erase();

                      shape = ArtTracer.factory("square");
                      shape.draw();
                      shape.erase();

                      shape = ArtTracer.factory("triangle");
                      shape.draw();
                      shape.erase();

                      //未知圖形類型
                      shape = ArtTracer.factory("unknownShape");
                      shape.draw();
                      shape.erase();
                  }catch(BadShapeException e){
                      System.out.println(e.getMessage());
                  }
              }
          }

          /**
           *簡(jiǎn)單工廠:繪圖員
           */
          class ArtTracer{
              public static Shape factory(String type)throws BadShapeException{
                  if(type.equalsIgnoreCase("Circle")){
                      return new Circle();
                  }else if(type.equalsIgnoreCase("Square")){
                      return new Square();
                  }else if(type.equalsIgnoreCase("Triangle")){
                      return new Triangle();
                  }else{
                      throw new BadShapeException(type);
                  }
              }
          }

          /**
           *抽象產(chǎn)品
           */
          abstract class Shape{
              public abstract void draw();
              public abstract void erase();
          }

          /**
           *具體產(chǎn)品
           */
          class Circle extends Shape{
             
              public void draw(){
                  System.out.println("Circle draw");
              }

             
              public void erase(){
                  System.out.println("Circle erase");
              }
          }

          /**
           *具體產(chǎn)品
           */
          class Square extends Shape{
             
              public void draw(){
                  System.out.println("Square draw");
              }

             
              public void erase(){
                  System.out.println("Square erase");
              }
          }

          /**
           *具體產(chǎn)品
           */
          class Triangle extends Shape{
             
              public void draw(){
                  System.out.println("Triangle draw");
              }

             
              public void erase(){
                  System.out.println("Triangle erase");
              }
          }

          /**
           *輔助類:當(dāng)輸入的類為不支持的圖形時(shí),拋出異常
           */
          class BadShapeException extends Exception{
              public BadShapeException(String type){
                  super(type + ":不支持的圖形");
              }
          }
          posted on 2009-04-14 12:26 天堂露珠 閱讀(1074) 評(píng)論(2)  編輯  收藏 所屬分類: Pattern

          FeedBack:
          # re: [原]簡(jiǎn)單工廠模式 2009-04-14 17:55 YXY
          加上動(dòng)態(tài)加載可以使工廠模式更有彈性:

          class Factory {
          public static Fruit getFruit(String className) {
          Fruit f = null;
          try {
          f = (Fruit) Class.forName(className).newInstance();
          } catch (Exception e) {
          e.printStackTrace();
          }
          return f;
          }
          }
            回復(fù)  更多評(píng)論
            
          # re: [原]簡(jiǎn)單工廠模式 2009-04-14 18:58 天堂露珠
          @YXY
          不錯(cuò)。  回復(fù)  更多評(píng)論
            
          主站蜘蛛池模板: 图们市| 邯郸县| 从化市| 泽普县| 伊宁市| 鄂尔多斯市| 苏尼特右旗| 大荔县| 江都市| 镇原县| 兴宁市| 阳高县| 邹城市| 麻阳| 桑植县| 筠连县| 民丰县| 阳高县| 尖扎县| 大同市| 昭通市| 大方县| 富源县| 重庆市| 上蔡县| 九龙县| 穆棱市| 马公市| 资源县| 新津县| 宜兴市| 新蔡县| 定结县| 海林市| 德州市| 汉寿县| 鹤山市| 博客| 怀集县| 岑溪市| 剑河县|