Flyingis

          Talking and thinking freely !
          Flying in the world of GIS !
          隨筆 - 156, 文章 - 16, 評(píng)論 - 589, 引用 - 0
          數(shù)據(jù)加載中……

          [Design Pattern] The Factory Pattern

              作者:Flyingis

              工廠模式用于創(chuàng)建實(shí)例對(duì)象,我們只需告訴工廠需要的對(duì)象有什么特點(diǎn),就能得到所需的結(jié)果,而不用去關(guān)心怎么創(chuàng)建對(duì)象,工廠類(lèi)似于黑盒,黑盒里面關(guān)于對(duì)象生產(chǎn)的細(xì)節(jié)不是關(guān)注的重點(diǎn)。

              工廠模式分為:簡(jiǎn)單工廠模式、工廠模式、抽象工廠模式。

              例子:Thinkpad筆記本生產(chǎn)線。

              簡(jiǎn)單工廠模式

          public class SimpleThinkpadTFactory {
            
          public ThinkpadT produceThinkpadT(String type) {
              ThinkpadT thinkpadT 
          = null;
              
          // 根據(jù)不同的類(lèi)型創(chuàng)建不同的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;
              
          // 不再根據(jù)不同條件,使用new去創(chuàng)建對(duì)象
              thinkpadT = factory.produceThinkpadT(type);
              
          return thinkpadT;
            }

          }


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

              工廠模式

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

            
          // 單獨(dú)抽取出工廠方法,abstract類(lèi)型,需要在子類(lèi)中實(shí)現(xiàn)
            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;
            }

          }


              具體執(zhí)行代碼:

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

              ThinkpadT thinkpadT 
          = null;
              
          // 購(gòu)買(mǎi)Thinkpad T43筆記本
              thinkpadT = thinkpadT43Store.buyThinkpadT("T43");
              
          // 購(gòu)買(mǎi)Thinkpad T60p筆記本
              thinkpadT = thinkpadT60Store.buyThinkpadT("T60p");
            }

          }


              這樣就將不同型號(hào)T系列筆記本的生產(chǎn)進(jìn)行了更細(xì)的劃分,降低了簡(jiǎn)單工廠中工廠類(lèi)的耦合程度,抽取出來(lái)的各種Store只用關(guān)心一種型號(hào)筆記本的生產(chǎn),如T43或T60。

              工廠模式的抽象結(jié)構(gòu)圖可以表示如下:


              (上圖摘自Head First Patterns)

              抽象工廠模式

              什么時(shí)候需要使用抽象工廠模式呢?抽象工廠模式用戶生產(chǎn)線更復(fù)雜的情況下,例如現(xiàn)在除了T系列的Thinkpad筆記本,我們還需要生產(chǎn)R系列和X系列的產(chǎn)品,這時(shí)就需要更多的工廠來(lái)負(fù)責(zé)不同系列Thinkpad的生產(chǎn)。


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

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

              白話設(shè)計(jì)模式--Abstract Factory

              C#設(shè)計(jì)模式(6)--Abstract Factory Pattern

          posted on 2007-06-17 15:01 Flyingis 閱讀(3645) 評(píng)論(2)  編輯  收藏 所屬分類(lèi): 架構(gòu)與設(shè)計(jì)

          評(píng)論

          # re: [Design Pattern] The Factory Pattern[未登錄](méi)  回復(fù)  更多評(píng)論   

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

          # re: [Design Pattern] The Factory Pattern  回復(fù)  更多評(píng)論   

          發(fā)布隨筆時(shí),編輯器工具欄上有“插入代碼”,支持多種語(yǔ)言的編輯格式。
          2007-06-20 13:46 | Flyingis
          主站蜘蛛池模板: 老河口市| 富川| 于田县| 时尚| 延吉市| 南投县| 郯城县| 桦甸市| 雷州市| 江安县| 贵州省| 博爱县| 巴彦淖尔市| 鹤壁市| 阳谷县| 大悟县| 海伦市| 贡觉县| 驻马店市| 香河县| 华池县| 茶陵县| 涞源县| 监利县| 青海省| 永胜县| 余干县| 富民县| 奉贤区| 安陆市| 得荣县| 米泉市| 娄烦县| 长岭县| 岳西县| 合肥市| 平度市| 吉林省| 台北县| 济源市| 三明市|