<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Flyingis

    Talking and thinking freely !
    Flying in the world of GIS !
    隨筆 - 156, 文章 - 16, 評論 - 589, 引用 - 0
    數據加載中……

    [Design Pattern] The Factory Pattern

        作者:Flyingis

        工廠模式用于創建實例對象,我們只需告訴工廠需要的對象有什么特點,就能得到所需的結果,而不用去關心怎么創建對象,工廠類似于黑盒,黑盒里面關于對象生產的細節不是關注的重點。

        工廠模式分為:簡單工廠模式、工廠模式、抽象工廠模式。

        例子:Thinkpad筆記本生產線。

        簡單工廠模式

    public class SimpleThinkpadTFactory {
      
    public ThinkpadT produceThinkpadT(String type) {
        ThinkpadT thinkpadT 
    = null;
        
    // 根據不同的類型創建不同的Thinkpad
        if (type.equals("t60")) {
          thinkpadT 
    = new ThinkpadT60();
        }
     else if (type.equals("t60p")) {
          thinkpadT 
    = new ThinkpadT60p();
        }

      
    return thinkpadT;
      }

    }


    public class ThinkpadTStore {
      SimpleThinkpadTFactory factory;
      
    public ThinkpadTStore(SimpleThinkpadFactory factory) {
        
    this.factory = factory;
      }


      
    public ThinkpadT buyThinkpadT(String type) {
        ThinkpadT thinkpadT;
        
    // 不再根據不同條件,使用new去創建對象
        thinkpadT = factory.produceThinkpadT(type);
        
    return thinkpadT;
      }

    }


        從上例可以看出,SimpleThinkpadTFactory只需知道用戶需要什么型號的Thinkpad T系列的電腦,就能返回該型號Thinkpad的對象,避免了在ThinkpadTStore中書寫冗長的代碼,降低了代碼的耦合度。但是在SimpleThinkpadTFactory中,一旦機器的型號分的特別細、特別多,如T42/T43/T60/T42p/T43p/T60p等等,就需要維護大量的"if else",這顯然不是我們想看到的,這里我們引入工廠模式。

        工廠模式

    public abstract class ThinkpadTStore {
      
    public ThinkpadT buyThinkpadT(String type) {
        ThinkpadT thinkpadT;
        thinkpadT 
    = produceThinkpadT(type);
        
    return thinkpadT;
      }

      
    // 單獨抽取出工廠方法,abstract類型,需要在子類中實現
      abstract produceThinkpadT(String type);
    }


    public class ThinkpadT43Store extends ThinkpadTStore {
      ThinkpadT produceThinkpadT(String type) 
    {
        
    if (type.equals("T43")) {
          
    return new ThinpadT43();
        }
     else if (type.equals("T43p")) {
          
    return new ThinkpadT43p();
        }
     else return null;
      }

    }


    public class ThinkpadT60Store extends ThinkpadTStore {
      ThinkpadT produceThinkpadT(String type) 
    {
        
    if (type.equals("T60")) {
          
    return new ThinpadT60();
        }
     else if (type.equals("T60p")) {
          
    return new ThinkpadT60p();
        }
     else return null;
      }

    }


        具體執行代碼:

    public class ThinpadTest {
      
    public static void main(String[] args) {
        ThinkpadTStore thinkpadT43Store 
    = new ThinkpadT43Store();
        ThinkpadTStore thinkpadT60Store 
    = new ThinkpadT60Store();

        ThinkpadT thinkpadT 
    = null;
        
    // 購買Thinkpad T43筆記本
        thinkpadT = thinkpadT43Store.buyThinkpadT("T43");
        
    // 購買Thinkpad T60p筆記本
        thinkpadT = thinkpadT60Store.buyThinkpadT("T60p");
      }

    }


        這樣就將不同型號T系列筆記本的生產進行了更細的劃分,降低了簡單工廠中工廠類的耦合程度,抽取出來的各種Store只用關心一種型號筆記本的生產,如T43或T60。

        工廠模式的抽象結構圖可以表示如下:


        (上圖摘自Head First Patterns)

        抽象工廠模式

        什么時候需要使用抽象工廠模式呢?抽象工廠模式用戶生產線更復雜的情況下,例如現在除了T系列的Thinkpad筆記本,我們還需要生產R系列和X系列的產品,這時就需要更多的工廠來負責不同系列Thinkpad的生產。


        (上圖摘自呂震宇的博客)   

        有兩篇文章對于抽象工廠模式闡述的非常好:

        白話設計模式--Abstract Factory

        C#設計模式(6)--Abstract Factory Pattern

    posted on 2007-06-17 15:01 Flyingis 閱讀(3639) 評論(2)  編輯  收藏 所屬分類: 架構與設計

    評論

    # re: [Design Pattern] The Factory Pattern[未登錄]  回復  更多評論   

    請問樓主,文章中的可折疊的源代碼是怎樣生成的?是有工具生成的嗎?
    2007-06-20 13:29 | allen

    # re: [Design Pattern] The Factory Pattern  回復  更多評論   

    發布隨筆時,編輯器工具欄上有“插入代碼”,支持多種語言的編輯格式。
    2007-06-20 13:46 | Flyingis
    主站蜘蛛池模板: 久久久久久成人毛片免费看| 日本黄页网址在线看免费不卡| 国产在线观a免费观看| 国产又黄又爽又刺激的免费网址 | 中文字幕亚洲综合久久男男| 亚洲av永久无码| 国产又长又粗又爽免费视频| 处破女第一次亚洲18分钟| 四虎永久免费影院在线| 色吊丝性永久免费看码| 男人的天堂亚洲一区二区三区| 精品亚洲AV无码一区二区三区 | 亚洲AV伊人久久青青草原| 爱情岛论坛亚洲品质自拍视频网站| 最新69国产成人精品免费视频动漫 | 国产亚洲一区二区三区在线| 中国内地毛片免费高清| 国产成人亚洲精品青草天美| 桃子视频在线观看高清免费视频| 亚洲国产人成在线观看69网站| 1000部夫妻午夜免费| 亚洲久悠悠色悠在线播放| 国产免费变态视频网址网站| 四虎成人精品国产永久免费无码 | 香蕉免费在线视频| 久久久久亚洲AV无码专区首| 久久www免费人成看片| 亚洲午夜精品久久久久久app | 久久亚洲AV午夜福利精品一区| ww在线观视频免费观看| 色欲aⅴ亚洲情无码AV蜜桃| 一区二区三区亚洲视频| 国产羞羞的视频在线观看免费| 亚洲国产精品人久久电影| 成年女人18级毛片毛片免费观看| 免费无码专区毛片高潮喷水| 亚洲国产精品久久久久婷婷软件 | 亚洲视频2020| 国产小视频在线免费| 久久免费国产精品一区二区| 中中文字幕亚洲无线码|