隨筆-46  評論-64  文章-2  trackbacks-0

          設計模式學習(一) 工廠模式之簡單工廠

          Creational Pattern:

          *creates objects for you rather than having you instantiate objects directly

          *gives your program more flexibility in deciding which objects need to be created for a given case

           

          工廠模式有以下三種形態:

          簡單工廠(Simple Factory)模式:又稱靜態工廠方法(Static Factory Method)模式

          工廠方法(Factory Method)模式:又稱多態性工廠(Polymorphic Factory)模式

          抽象工廠(Abstract Factory)模式:又稱工具箱(Kit Toolkit)模式

           

          簡單工廠模式其實是普通工廠模式的一個特例,今天就從這里開始吧。

          其結構可以簡單地表示如下:


          SimpleFactory.jpg沒用
          Visio畫,大家見諒呀


              我們從一個實際的例子來看這個簡單工廠模式

          假設一個農場,專門向市場銷售各種水果,假設只提供良種的水果,蘋果和葡萄,我們為水果設計一個抽象類Fruit,所有水果都必須實現這個接口

          package simple_Factory;
          //水果抽象出來的接口
          public interface Fruit {
              
          void grow();
              
          void harvest();
          }


          public class Apple implements Fruit {

              
          private int treeAge;

              
          public void grow() {
                  log(
          "Apple is glowing");
                  
              }


              
          private void log(String string) {
                  System.out.println(string);        
              }


              
          public void harvest() {
                  log(
          "Apple has been harvested.");
              }

              
              
          public int getTreeAge() {
                  
          return treeAge;
              }


              
          public void setTreeAge(int treeAge) {
                  
          this.treeAge = treeAge;
              }


          }


          public class Grape implements Fruit {

              
          private boolean seedless;
              
          public void grow() {
                  log(
          "Grape is growing------");
              }


              
              
          public void harvest() {
                  log(
          "Grape has been harvested.");
              }

              
          private void log(String string) {
                  System.out.println(string);        
              }


              
          public boolean isSeedless() {
                  
          return seedless;
              }


              
          public void setSeedless(boolean seedless) {
                  
          this.seedless = seedless;
              }


          }


          public class OtherFruits implements Fruit {

              
          public void grow() {        
              }


              
          public void harvest() {        
              }


          }

           

          FruitFactory類,水果加工廠,根據需要(不同參數代表不同的水果需求)給市場供給水果。

           

          package simple_Factory;

          //水果加工廠,根據需要給市場供給水果
          public class FruitFactory {
              
          public static Fruit supplyFruit(String need)
              
          {
                  
          if(need.equalsIgnoreCase("apple"))
                      
          return new Apple();
                  
          else if(need.equalsIgnoreCase("grape"))
                      
          return new Grape();
                  
          else
                      
          return new OtherFruits();        
              }

          }

          測試方法:
          package simple_Factory;

          public class Test {

              
          /**
               * 
          @param args
               
          */

              
          public static void main(String[] args) {
                  Fruit a 
          = FruitFactory.supplyFruit("apple");
                  Fruit b 
          = FruitFactory.supplyFruit("Grape");
                  Fruit c 
          = FruitFactory.supplyFruit("others");
                  
                  a.grow();a.harvest();
                  b.grow();b.harvest();
                  c.grow();c.harvest();
              }

          }


              自己弄懂和講給別人懂還是有很大差距的,第一篇文章雖然寫好了,但是感覺不夠好,不知道能不能給初學者一點點幫助呢……

              自強不息,繼續努力!
          posted on 2006-02-21 22:03 jht 閱讀(2193) 評論(7)  編輯  收藏

          評論:
          # re: 設計模式學習(一) 工廠模式之簡單工廠 2006-02-21 23:48 | 飛雪
          不錯
          繼續加油!  回復  更多評論
            
          # re: 設計模式學習(一) 工廠模式之簡單工廠 2006-02-22 08:29 | java哥哥
          寫的挺好,繼續。  回復  更多評論
            
          # re: 設計模式學習(一) 工廠模式之簡單工廠 2006-02-22 12:47 | david
          閻宏的書里講的例子吧  回復  更多評論
            
          # re: 設計模式學習(一) 工廠模式之簡單工廠 2006-02-22 16:21 | steady
          《Java與模式》里的例子了  回復  更多評論
            
          # re: 設計模式學習(一) 工廠模式之簡單工廠 2006-07-29 20:18 | 支票薄
          雖然見過
          但表達比原著更好。共勉!!!  回復  更多評論
            
          # re: 設計模式學習(一) 工廠模式之簡單工廠 2006-08-12 19:49 | 謝謝樓主,寫的不錯
          謝謝樓主,寫的不錯!
          希望與樓主交流
          MSN:sboy824@hotmail.com
          QQ:43713757  回復  更多評論
            
          # re: 設計模式學習(一) 工廠模式之簡單工廠 2006-11-01 16:53 | leo[匿名]
          怎么設置蘋果樹的年齡呢????????  回復  更多評論
            

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 红安县| 巫溪县| 梅州市| 隆化县| 客服| 成都市| 乐陵市| 贞丰县| 乌兰浩特市| 绍兴市| 荥经县| 西畴县| 卢龙县| 宁德市| 巩义市| 莲花县| 德清县| 闽侯县| 高邮市| 安国市| 健康| 五指山市| 上高县| 潞城市| 九寨沟县| 安龙县| 怀安县| 台湾省| 陇川县| 金坛市| 元阳县| 多伦县| 开江县| 西贡区| 镇赉县| 梁平县| 房产| 阳曲县| 农安县| 阳信县| 响水县|