隨筆-46  評(píng)論-64  文章-2  trackbacks-0

          設(shè)計(jì)模式學(xué)習(xí)(一) 工廠模式之簡(jiǎn)單工廠

          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):

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

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

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

           

          簡(jiǎn)單工廠模式其實(shí)是普通工廠模式的一個(gè)特例,今天就從這里開始吧。

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


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


              我們從一個(gè)實(shí)際的例子來看這個(gè)簡(jiǎn)單工廠模式

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

          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ù)代表不同的水果需求)給市場(chǎng)供給水果。

           

          package simple_Factory;

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

          }

          測(cè)試方法:
          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();
              }

          }


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

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

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

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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 会同县| 呼图壁县| 新津县| 太谷县| 南丹县| 长沙市| 乌兰察布市| 馆陶县| 阿坝县| 民乐县| 穆棱市| 吴川市| 东辽县| 桃源县| 平顺县| 尉氏县| 增城市| 潜山县| 蕉岭县| 牡丹江市| 星座| 喜德县| 平顶山市| 伽师县| 平安县| 永德县| 枞阳县| 剑川县| 金坛市| 简阳市| 收藏| 连城县| 贵州省| 泗洪县| 太白县| 石河子市| 马公市| 汤原县| 乌拉特后旗| 河曲县| 凤凰县|