e代劍客——溫柔一刀

          生活就像海洋,只有意志堅(jiān)強(qiáng)的人,才能到達(dá)彼岸

             :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            76 隨筆 :: 7 文章 :: 215 評(píng)論 :: 0 Trackbacks

          簡單工廠模式

          1. 目的?
          ?????? ?工廠模式就是專門負(fù)責(zé)將大量有共同接口的類實(shí)例化,而且不必事先知道每次是要實(shí)例化哪一個(gè)類的模式。它定義一個(gè)用于創(chuàng)建對(duì)象的接口,由子類決定實(shí)例化哪一個(gè)類。
          2 . 簡單工廠模式的結(jié)構(gòu)?
          ?

          3. 一個(gè)簡單例子

          // 產(chǎn)品接口?
          public ? interface ?Product? {?

          ?????
          public ? void ?getName()?;?

          }
          ?
          // 具體產(chǎn)品A?
          public ? class ?ProductA? implements ?Product? {?

          ???
          public ? void ?getName()? {?
          ???????System.out.println(
          " ?I?am?ProductA? " ?);?
          ??}
          ?

          }
          ?
          // 具體產(chǎn)品B?
          public ? class ?ProductB? implements ?Product? {?

          ???
          public ? void ?getName()? {?
          ?????????System.out.println(
          " ?I?am?ProductB? " ?);?
          ??}
          ?

          }
          ?
          // ?工廠類?
          public ? class ?ProductCreator? {?

          public ?Product?createProduct(String?type)? {?
          ?????
          if ?(? " A " .equals(type)?)? {?
          ????????????
          return ? new ?ProductA();?
          ????}
          ?
          ????
          if ?(? " B " .equals(type)?)? {?
          ??????????
          return ? new ?ProductB();?
          ???}
          ?
          ???
          else ?
          ?????????
          return ? null ;?
          }
          ?

          public ? static ? void ?main(String[]?args)? {?
          ??????ProductCreator?creator?
          = ? new ?ProductCreator();?
          ??????creator.createProduct(
          " A " ).getName();?
          ??????creator.createProduct(
          " B " ).getName();?
          ?}
          ?
          }
          ?


          4. 小結(jié)工廠模式的適用范圍
          ? 在編碼時(shí)不能預(yù)見需要?jiǎng)?chuàng)建哪一種類的實(shí)例。
          ? 一個(gè)類使用它的子類來創(chuàng)建對(duì)象。
          ? 開發(fā)人員不希望創(chuàng)建了哪個(gè)類的實(shí)例以及如何創(chuàng)建實(shí)例的信息暴露給外部程序。?


          ?

          ?

          抽象工廠模式

          ?

          1. 抽象工廠模式可以說是簡單工廠模式的擴(kuò)展,它們主要的區(qū)別在于需要?jiǎng)?chuàng)建對(duì)象的復(fù)雜程度上。
          在抽象工廠模式中,抽象產(chǎn)品可能是一個(gè)或多個(gè),從而構(gòu)成一個(gè)或多個(gè)產(chǎn)品族。 在只有一個(gè)產(chǎn)品族的情況下,抽象工廠模式實(shí)際上退化到工廠方法模式。
          2. 抽象工廠模式的結(jié)構(gòu)?

          3. 一個(gè)簡單例子

          // ?產(chǎn)品?Plant接口?
          public ? interface ?Plant? {?
          }
          ?
          // 具體產(chǎn)品PlantA,PlantB?
          public ? class ?PlantA? implements ?Plant? {?

          ??
          public ?PlantA?()? {?
          ????System.out.println(
          " create?PlantA?! " );?
          ??}
          ?

          ??
          public ? void ?doSomething()? {?
          ?????System.out.println(
          " ?PlantA?do?something? " );?
          ??}
          ?
          }
          ?
          public ? class ?PlantB? implements ?Plant? {?
          ??
          public ?PlantB?()? {?
          ?????System.out.println(
          " create?PlantB?! " );?
          ??}
          ?

          ??
          public ? void ?doSomething()? {?
          ?????System.out.println(
          " ?PlantB?do?something? " );?
          ??}
          ?
          }
          ?
          // ?產(chǎn)品?Fruit接口?
          public ? interface ?Fruit? {?
          }
          ?
          // 具體產(chǎn)品FruitA,F(xiàn)ruitB?
          public ? class ?FruitA? implements ?Fruit? {?
          ??
          public ?FruitA()? {?
          ????System.out.println(
          " create?FruitA?! " );?
          ??}
          ?
          ??
          public ? void ?doSomething()? {?
          ????System.out.println(
          " ?FruitA?do?something? " );?
          ??}
          ?
          }
          ?
          public ? class ?FruitB? implements ?Fruit? {?
          ??
          public ?FruitB()? {?
          ????System.out.println(
          " create?FruitB?! " );?
          ??}
          ?
          ??
          public ? void ?doSomething()? {?
          ????System.out.println(
          " ?FruitB?do?something? " );?
          ??}
          ?
          }
          ?
          // ?抽象工廠方法?
          public ? interface ?AbstractFactory? {?
          ??
          public ?Plant?createPlant();?
          ??
          public ?Fruit?createFruit()?;?
          }
          ?
          // 具體工廠方法?
          public ? class ?FactoryA? implements ?AbstractFactory? {?
          ??
          public ?Plant?createPlant()? {?
          ????
          return ? new ?PlantA();?
          ??}
          ?
          ??
          public ?Fruit?createFruit()? {?
          ????
          return ? new ?FruitA();?
          ??}
          ?
          }
          ?
          public ? class ?FactoryB? implements ?AbstractFactory? {?
          ??
          public ?Plant?createPlant()? {?
          ????
          return ? new ?PlantB();?
          ??}
          ?
          ??
          public ?Fruit?createFruit()? {?
          ????
          return ? new ?FruitB();?
          ??}
          ?
          }
          ?


          4. 小結(jié)
          在以下情況下,應(yīng)當(dāng)考慮使用抽象工廠模式。
            首先,一個(gè)系統(tǒng)應(yīng)當(dāng)不依賴于產(chǎn)品類實(shí)例被創(chuàng)立,組成,和表示的細(xì)節(jié)。這對(duì)于所有形態(tài)的工廠模式都是重要的。
            其次,這個(gè)系統(tǒng)的產(chǎn)品有多于一個(gè)的產(chǎn)品族。
            第三,同屬于同一個(gè)產(chǎn)品族的產(chǎn)品是設(shè)計(jì)成在一起使用的。這一約束必須得在系統(tǒng)的設(shè)計(jì)中體現(xiàn)出來。
            最后,不同的產(chǎn)品以一系列的接口的面貌出現(xiàn),從而使系統(tǒng)不依賴于接口實(shí)現(xiàn)的細(xì)節(jié)。
            其中第二丶第三個(gè)條件是我們選用抽象工廠模式而非其它形態(tài)的工廠模式的關(guān)鍵性條件。

          posted on 2006-07-13 22:30 溫柔一刀 閱讀(270) 評(píng)論(0)  編輯  收藏 所屬分類: java相關(guān)
          聯(lián)系偶 zhupanjava@gmail.com 溫柔一刀
          主站蜘蛛池模板: 张家口市| 铅山县| 铁岭市| 那曲县| 宁化县| 张掖市| 沾益县| 乐亭县| 永定县| 子洲县| 沙河市| 启东市| 抚顺县| 章丘市| 图们市| 柳林县| 板桥市| 霍邱县| 太仆寺旗| 德格县| 蒙阴县| 兴城市| 满洲里市| 丰原市| 清徐县| 利川市| 江源县| 南溪县| 隆化县| 宜良县| 兰溪市| 扶沟县| 泾阳县| 昆山市| 应城市| 田东县| 阿拉尔市| 于都县| 大埔县| 宾阳县| 武隆县|