積少成多

          垃圾堆

            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)  編輯  收藏 所屬分類: 模式

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


          網站導航:
           
          主站蜘蛛池模板: 彩票| 同心县| 宣武区| 图们市| 广水市| 邹平县| 阿图什市| 隆林| 潢川县| 宝清县| 明光市| 延安市| 靖州| 鲜城| 专栏| 称多县| 连南| 景泰县| 郁南县| 丰顺县| 宁德市| 来安县| 南和县| 娱乐| 定安县| 五台县| 曲阳县| 金川县| 波密县| 临西县| 伽师县| 恩施市| 厦门市| 宜兰市| 镇雄县| 涞水县| 昌图县| 祁东县| 中卫市| 新闻| 延寿县|