我的漫漫程序之旅

          專注于JavaWeb開發
          隨筆 - 39, 文章 - 310, 評論 - 411, 引用 - 0
          數據加載中……

          factory模式的學習

          1.Simple Factory模式:

          1、 在這里,我們先定義水果(Fruit)接口:


          package com.pattern.simplefactory;

          public interface Fruit
          {
              
          void plant();

              
          void enableEat();
          }

          2、 蘋果(Apple)是對水果(Fruit)接口的實現:


          package com.pattern.simplefactory;

          public class Apple implements Fruit
          {

              
          public void enableEat()
              {
                  System.out.println(
          "蘋果能吃啦");
              }

              
          public void plant()
              {
                  System.out.println(
          "種植蘋果");
              }
              
          }

          3、 葡萄(Grape)是對水果(Fruit)接口的實現:


          package com.pattern.simplefactory;

          public class Grape implements Fruit
          {

              
          public void enableEat()
              {
                  System.out.println(
          "葡萄能吃啦");
              }

              
          public void plant()
              {
                  System.out.println(
          "種植葡萄");
              }

          }

          4、 鴨梨(Pear)是對水果(Fruit)接口的實現:


          package com.pattern.simplefactory;

          public class Pear implements Fruit
          {

              
          public void enableEat()
              {
                  System.out.println(
          "鴨梨能吃啦!");
              }

              
          public void plant()
              {
                  System.out.println(
          "種植鴨梨");
              }

          }

          5、定義買水果工廠(FruitFactory)這一過程類:


          package com.pattern.simplefactory;

          public class FruitFactory
          {
              
          /**
               * 種植水果
               * 
               * 
          @param str
               * 
          @return
               
          */
              
          public static Fruit plantFruit(String str)
              {
                  str 
          = str.toLowerCase();
                  
          if (str.equals("apple"))
                      
          return new Apple();
                  
          if (str.equals("grape"))
                      
          return new Grape();
                  
          if (str.equals("pear"))
                      
          return new Pear();
                  
          return null;
              }

              
          /**
               * 吃水果
               * 
               * 
          @param str
               * 
          @return
               
          */
              
          public static Fruit eatFruit(String str)
              {
                  str 
          = str.toLowerCase();
                  
          if (str.equals("apple"))
                      
          return new Apple();
                  
          if (str.equals("grape"))
                      
          return new Grape();
                  
          if (str.equals("pear"))
                      
          return new Pear();
                  
          return null;
              }
          }

          6、 編寫測試類:


          package com.pattern.simplefactory;

          public class Client
          {
              
          public static void main(String[] args)
              {
                  Apple apple 
          = (Apple) FruitFactory.plantFruit("apple");
                  apple.plant();
                  
                  Apple appleEat 
          = (Apple) FruitFactory.eatFruit("apple");
                  appleEat.enableEat();
                  
                  Grape grape 
          = (Grape) FruitFactory.plantFruit("grape");
                  grape.plant();
                  
                  Grape grapeEat 
          = (Grape) FruitFactory.eatFruit("grape");
                  grapeEat.enableEat();
                  
                  
                  Pear pear 
          = (Pear) FruitFactory.plantFruit("pear");
                  pear.plant();
                  
                  Pear pearEat 
          = (Pear) FruitFactory.eatFruit("pear");
                  pearEat.enableEat();
              }
          }

          7、 說明:

          A:我要購買蘋果,只需向工廠角色(BuyFruit)請求即可。而工廠角色在接到請求后,會自行判斷創建和提供哪一個產品。

          B:但是對于工廠角色(FruitFactory)來說,增加新的產品(比如說增加草莓)就是一個痛苦的過程。工廠角色必須知道每一種產品,如何創建它們,以及何時向客戶端提供它們。換言之,接納新的產品意味著修改這個工廠。

          C:因此Simple Factory模式的開放性比較差。

          有什么辦法可以解決這個問題嗎?那就需要Factory Method模式來為我們服務了。

          二、Factory Method模式:

          1、同樣,我們先定義水果(Fruit)接口:


          package com.pattern.factorymethod;

          public interface Fruit
          {
              
          void plant();
          }

          2、蘋果(Apple)是對水果(Fruit)接口的實現:


          package com.pattern.factorymethod;

          public class Apple implements Fruit
          {

              
          public void plant()
              {
                  System.out.println(
          "plant apple");
              }
              
          }

          3、葡萄(Grape)是對水果(Fruit)接口的實現:


          package com.pattern.factorymethod;

          public class Grape implements Fruit
          {

              
          public void plant()
              {
                  System.out.println(
          "plant grape");
              }

          }

          4、鴨梨(Pear)是對水果(Fruit)接口的實現:


          package com.pattern.factorymethod;

          public class Pear implements Fruit
          {

              
          public void plant()
              {
                  System.out.println(
          "plant pear");
              }

          }

          5、在這里我們將買水果(FruitFactory)定義為接口類:


          package com.pattern.factorymethod;

          public interface FruitFactory
          {
              Fruit plantFruit();
          }

          6、種植蘋果是(PlantApple)對水果工廠(FruitFactory)這個接口的實現


          package com.pattern.factorymethod;

          import com.pattern.factorymethod.FruitFactory;

          public class PlantApple implements FruitFactory
          {

              
          public Fruit plantFruit()
              {
                  
          return new Apple();
              }

          }

          7、種植鴨梨是(PlantBear)對水果工廠(FruitFactory)這個接口的實現


          package com.pattern.factorymethod;

          public class PlantPear implements FruitFactory
          {
              
          public Fruit plantFruit()
              {
                  
          return new Pear();
              }

          }

          8、種植葡萄是(PlantGrape)對水果工廠(FruitFactory)這個接口的實現


          package com.pattern.factorymethod;

          public class PlantGrape implements FruitFactory
          {

              
          public Fruit plantFruit()
              {
                  
          return new Grape();
              }

          }

          9、編寫測試類:


          package com.pattern.factorymethod;
          /**
           * 測試類
           * 
          @author supercrsky
           *
           
          */
          public class Client
          {
              
          public static void main(String[] args)
              {
                  FruitFactory factory 
          = new PlantApple();
                  factory.plantFruit().plant();
                  
                  factory 
          = new PlantGrape();
                  factory.plantFruit().plant();
                  
                  factory 
          = new PlantPear();
                  factory.plantFruit().plant();
              }
          }

          10、說明:

          A:工廠方法模式和簡單工廠模式在結構上的不同是很明顯的。工廠方法模式的核心是一個抽象工廠類,而簡單工廠模式把核心放在一個具體類上。工廠方法模式可以允許很多具體工廠類從抽象工廠類中將創建行為繼承下來,從而可以成為多個簡單工廠模式的綜合,進而推廣了簡單工廠模式。

          B:工廠方法模式退化后可以變得很像簡單工廠模式。設想如果非常確定一個系統只需要一個具體工廠類,那么就不妨把抽象工廠類合并到具體的工廠類中去。由于反正只有一個具體工廠類,所以不妨將工廠方法改成為靜態方法,這時候就得到了簡單工廠模式。

          C:如果需要加入一個新的水果,那么只需要加入一個新的水果類以及它所對應的工廠類。沒有必要修改客戶端,也沒有必要修改抽象工廠角色或者其他已有的具體工廠角色。對于增加新的水果類而言,這個系統完全支持“開-閉”原則。

          D:對Factory Method模式而言,它只是針對一種類別(如本例中的水果類Fruit),但如果我們還想買肉,那就不行了,這是就必須要Abstract Factory Method模式幫忙了。


          三、Abstract Factory模式

          1、同樣,我們先定義水果(Fruit)接口:


          package com.pattern.abstractfactory;

          public interface Fruit
          {
              
          void plant();
          }

          2、蘋果(Apple)是對水果(Fruit)接口的實現:


          package com.pattern.abstractfactory;

          public class Apple implements Fruit
          {
              
          public void plant()
              {
                  System.out.println(
          "種植蘋果");
              }
          }

          2、 定義肉(Meat)接口:


          package com.pattern.abstractfactory;

          public interface Meat
          {
              
          void feed();
          }

          3、 豬肉(PigMeat)是對肉(Meat)接口的實現:


          package com.pattern.abstractfactory;

          public class PigMeat implements Meat
          {

              
          public void feed()
              {
                  System.out.println(
          "豬肉");
              }

          }

          4、 牛肉(CowMeat)是對肉(Meat)接口的實現:


          package com.pattern.abstractfactory;

          public class CowMeat implements Meat
          {

              
          public void feed()
              {
                  System.out.println(
          "牛肉");
              }

          }

          5、 我們可以定義工廠接口:


          package com.pattern.abstractfactory;

          public interface Factory
          {
              
          // 種水果
              Fruit plantFruit(Fruit fruit);

              
          // 養肉
              Meat feedMeat(Meat meat);
          }

          6、 我(MyFactory)是對Factory接口的實現:


          package com.pattern.abstractfactory;

          public class MyFactory implements Factory
          {
              
          // 養肉的工廠方法
              public Meat feedMeat(Meat meat)
              {
                  
          return meat;
              }

              
          // 種水果的工廠方法
              public Fruit plantFruit(Fruit fruit)
              {
                  
          return fruit;
              }
          }

          7、編寫測試類:


          package com.pattern.abstractfactory;

          public class Client
          {
              
          public static void main(String[] args)
              {
                  Fruit apple 
          = new Apple();
                  Meat pigMeat 
          = new PigMeat();
                  Factory factory 
          = new MyFactory();
                  
          // 種植蘋果
                  factory.plantFruit(apple);
                  
          // 養殖豬肉
                  factory.feedMeat(pigMeat);
              }
          }

          8、說明:

          A:抽象工廠模式可以向客戶端提供一個接口,使得客戶端在不必指定產品的具體類型的情況下,創建多個產品族中的產品對象。這就是抽象工廠模式的用意。

          B抽象工廠模式是所有形態的工廠模式中最為抽象和最具一般性的一種形態。



          posted on 2008-05-23 16:03 々上善若水々 閱讀(618) 評論(0)  編輯  收藏 所屬分類: 設計模式

          主站蜘蛛池模板: 奎屯市| SHOW| 武穴市| 宜川县| 崇明县| 玉田县| 醴陵市| 博客| 沭阳县| 九寨沟县| 辛集市| 辽源市| 枞阳县| 江阴市| 南充市| 丰顺县| 婺源县| 牡丹江市| 翼城县| 兴隆县| 遵义市| 海门市| 巴林右旗| 富源县| 上饶县| 邮箱| 永城市| 宿迁市| 鲁甸县| 定州市| 钦州市| 阿拉善盟| 磴口县| 颍上县| 雷山县| 静宁县| 怀安县| 乌兰浩特市| 剑川县| 怀集县| 丹巴县|