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 閱讀(3645) 評論(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
          主站蜘蛛池模板: 乌兰浩特市| 信丰县| 太仆寺旗| 延庆县| 永登县| 越西县| 格尔木市| 怀集县| 河间市| 高安市| 兴宁市| 宝丰县| 镇雄县| 阳春市| 明溪县| 绥芬河市| 敦化市| 郑州市| 富锦市| 中超| 中山市| 巴林右旗| 岢岚县| 美姑县| 宿松县| 兴义市| 朝阳市| 麦盖提县| 无为县| 上林县| 三都| 修水县| 新安县| 中山市| 宜州市| 龙川县| 武宣县| 化州市| 邻水| 安岳县| 治县。|