積少成多

          垃圾堆

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            29 Posts :: 1 Stories :: 5 Comments :: 0 Trackbacks
          工廠模式的產品角色和簡單工廠并沒有區別。變化就是在工廠角色那。將具體創建產品的工作放到子工廠進行。這樣就便于進行擴展了。
          public
           interface IProduct {
              
          public void desc();
          }

          public class ProductA implements IProduct {
              
          public void desc() {
                  
          //excute;
              }
          }

          public class ProductB implements IProduct {
              
          public void desc() {
                  
          //excute;
              }
          }

          public interface IFactory {
              
          public IProduct factory();
          }

          public class FactoryA implements IFactory {
              
          public IProduct factory() {
                  
          return new ProductA();
              }
          }

          public class FactoryB implements IFactory {
              
          public IProduct factory() {
                  
          return new ProductB();
              }
          }

          個人覺得如果工廠的factory方法如果是靜態的也可以阿。為什么不呢?但是接口是無法申明static的方法的。只能用類或抽象類來做。不知道還有沒有更好的方法。
          public abstract class IFactory {
              
          public static IProduct factory(){return null;};
          }

          public class FactoryA extends IFactory {
              
          public static IProduct factory() {
                  
          return new ProductA();
              }
          }

          public class FactoryB extends IFactory {
              
          public static IProduct factory() {
                  
          return new ProductB();
              }
          }


          posted on 2011-05-27 10:53 思無 閱讀(208) 評論(0)  編輯  收藏 所屬分類: 模式

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


          網站導航:
           
          主站蜘蛛池模板: 金山区| 旅游| 三明市| 青川县| 泾源县| 新巴尔虎右旗| 惠安县| 石景山区| 马龙县| 浙江省| 南开区| 玉山县| 平顶山市| 苗栗市| 怀仁县| 榆树市| 明光市| 台州市| 安义县| 阳谷县| 东丰县| 昆山市| 西乌珠穆沁旗| 苍南县| 祁门县| 新源县| 香格里拉县| 克拉玛依市| 静海县| 陇西县| 大荔县| 泰安市| 呼伦贝尔市| 莆田市| 子洲县| 盘锦市| 新乡市| 班戈县| 阿鲁科尔沁旗| 泾阳县| 沁源县|