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

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

          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

           

          工廠模式有以下三種形態(tài):

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

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

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

           

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

          其結(jié)構(gòu)可以簡單地表示如下:


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


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

          假設(shè)一個農(nóng)場,專門向市場銷售各種水果,假設(shè)只提供良種的水果,蘋果和葡萄,我們?yōu)樗O(shè)計一個抽象類Fruit,所有水果都必須實現(xiàn)這個接口

          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類,水果加工廠,根據(jù)需要(不同參數(shù)代表不同的水果需求)給市場供給水果。

           

          package simple_Factory;

          //水果加工廠,根據(jù)需要給市場供給水果
          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();
              }

          }


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

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

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

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


          網(wǎng)站導航:
           
          主站蜘蛛池模板: 石家庄市| 如皋市| 怀柔区| 清水河县| 勐海县| 元氏县| 呼伦贝尔市| 高密市| 渝北区| 常宁市| 彭泽县| 清苑县| 吴堡县| 南乐县| 泰顺县| 高清| 疏勒县| 扶绥县| 招远市| 积石山| 邵阳县| 井陉县| 玉林市| 鲜城| 紫金县| 卓尼县| 建湖县| 凤凰县| 大同市| 福鼎市| 茶陵县| 惠水县| 韩城市| 板桥市| 耿马| 读书| 汽车| 鹤庆县| 台江县| 成武县| 平阳县|