<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    e代劍客——溫柔一刀

    生活就像海洋,只有意志堅強的人,才能到達彼岸

       :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
      76 隨筆 :: 7 文章 :: 215 評論 :: 0 Trackbacks

    簡單工廠模式

    1. 目的?
    ?????? ?工廠模式就是專門負責將大量有共同接口的類實例化,而且不必事先知道每次是要實例化哪一個類的模式。它定義一個用于創建對象的接口,由子類決定實例化哪一個類。
    2 . 簡單工廠模式的結構?
    ?

    3. 一個簡單例子

    // 產品接口?
    public ? interface ?Product? {?

    ?????
    public ? void ?getName()?;?

    }
    ?
    // 具體產品A?
    public ? class ?ProductA? implements ?Product? {?

    ???
    public ? void ?getName()? {?
    ???????System.out.println(
    " ?I?am?ProductA? " ?);?
    ??}
    ?

    }
    ?
    // 具體產品B?
    public ? class ?ProductB? implements ?Product? {?

    ???
    public ? void ?getName()? {?
    ?????????System.out.println(
    " ?I?am?ProductB? " ?);?
    ??}
    ?

    }
    ?
    // ?工廠類?
    public ? class ?ProductCreator? {?

    public ?Product?createProduct(String?type)? {?
    ?????
    if ?(? " A " .equals(type)?)? {?
    ????????????
    return ? new ?ProductA();?
    ????}
    ?
    ????
    if ?(? " B " .equals(type)?)? {?
    ??????????
    return ? new ?ProductB();?
    ???}
    ?
    ???
    else ?
    ?????????
    return ? null ;?
    }
    ?

    public ? static ? void ?main(String[]?args)? {?
    ??????ProductCreator?creator?
    = ? new ?ProductCreator();?
    ??????creator.createProduct(
    " A " ).getName();?
    ??????creator.createProduct(
    " B " ).getName();?
    ?}
    ?
    }
    ?


    4. 小結工廠模式的適用范圍
    ? 在編碼時不能預見需要創建哪一種類的實例。
    ? 一個類使用它的子類來創建對象。
    ? 開發人員不希望創建了哪個類的實例以及如何創建實例的信息暴露給外部程序。?


    ?

    ?

    抽象工廠模式

    ?

    1. 抽象工廠模式可以說是簡單工廠模式的擴展,它們主要的區別在于需要創建對象的復雜程度上。
    在抽象工廠模式中,抽象產品可能是一個或多個,從而構成一個或多個產品族。 在只有一個產品族的情況下,抽象工廠模式實際上退化到工廠方法模式。
    2. 抽象工廠模式的結構?

    3. 一個簡單例子

    // ?產品?Plant接口?
    public ? interface ?Plant? {?
    }
    ?
    // 具體產品PlantA,PlantB?
    public ? class ?PlantA? implements ?Plant? {?

    ??
    public ?PlantA?()? {?
    ????System.out.println(
    " create?PlantA?! " );?
    ??}
    ?

    ??
    public ? void ?doSomething()? {?
    ?????System.out.println(
    " ?PlantA?do?something? " );?
    ??}
    ?
    }
    ?
    public ? class ?PlantB? implements ?Plant? {?
    ??
    public ?PlantB?()? {?
    ?????System.out.println(
    " create?PlantB?! " );?
    ??}
    ?

    ??
    public ? void ?doSomething()? {?
    ?????System.out.println(
    " ?PlantB?do?something? " );?
    ??}
    ?
    }
    ?
    // ?產品?Fruit接口?
    public ? interface ?Fruit? {?
    }
    ?
    // 具體產品FruitA,FruitB?
    public ? class ?FruitA? implements ?Fruit? {?
    ??
    public ?FruitA()? {?
    ????System.out.println(
    " create?FruitA?! " );?
    ??}
    ?
    ??
    public ? void ?doSomething()? {?
    ????System.out.println(
    " ?FruitA?do?something? " );?
    ??}
    ?
    }
    ?
    public ? class ?FruitB? implements ?Fruit? {?
    ??
    public ?FruitB()? {?
    ????System.out.println(
    " create?FruitB?! " );?
    ??}
    ?
    ??
    public ? void ?doSomething()? {?
    ????System.out.println(
    " ?FruitB?do?something? " );?
    ??}
    ?
    }
    ?
    // ?抽象工廠方法?
    public ? interface ?AbstractFactory? {?
    ??
    public ?Plant?createPlant();?
    ??
    public ?Fruit?createFruit()?;?
    }
    ?
    // 具體工廠方法?
    public ? class ?FactoryA? implements ?AbstractFactory? {?
    ??
    public ?Plant?createPlant()? {?
    ????
    return ? new ?PlantA();?
    ??}
    ?
    ??
    public ?Fruit?createFruit()? {?
    ????
    return ? new ?FruitA();?
    ??}
    ?
    }
    ?
    public ? class ?FactoryB? implements ?AbstractFactory? {?
    ??
    public ?Plant?createPlant()? {?
    ????
    return ? new ?PlantB();?
    ??}
    ?
    ??
    public ?Fruit?createFruit()? {?
    ????
    return ? new ?FruitB();?
    ??}
    ?
    }
    ?


    4. 小結
    在以下情況下,應當考慮使用抽象工廠模式。
      首先,一個系統應當不依賴于產品類實例被創立,組成,和表示的細節。這對于所有形態的工廠模式都是重要的。
      其次,這個系統的產品有多于一個的產品族。
      第三,同屬于同一個產品族的產品是設計成在一起使用的。這一約束必須得在系統的設計中體現出來。
      最后,不同的產品以一系列的接口的面貌出現,從而使系統不依賴于接口實現的細節。
      其中第二丶第三個條件是我們選用抽象工廠模式而非其它形態的工廠模式的關鍵性條件。

    posted on 2006-07-13 22:30 溫柔一刀 閱讀(259) 評論(0)  編輯  收藏 所屬分類: java相關
    聯系偶 zhupanjava@gmail.com 溫柔一刀
    主站蜘蛛池模板: 全免费a级毛片免费看无码| 国产免费久久精品丫丫| 最近中文字幕国语免费完整| 久久久久久久综合日本亚洲| 三年片免费高清版| 久久久久亚洲AV成人无码网站| a毛看片免费观看视频| 亚洲AV无码国产精品色午友在线| 丁香花在线视频观看免费 | 午夜国产精品免费观看| 久久精品国产亚洲av麻豆图片| 成人免费午夜无码视频| 亚洲欧洲免费无码| 免费日本黄色网址| 岛国岛国免费V片在线观看| 亚洲Av无码专区国产乱码DVD| 最近最好最新2019中文字幕免费 | 狠狠色伊人亚洲综合网站色| 日韩免费视频播播| 未满十八私人高清免费影院| 亚洲日韩精品A∨片无码| 18女人毛片水真多免费| 自拍偷区亚洲国内自拍| 亚洲精品一级无码中文字幕| 免费无码av片在线观看| 国产午夜亚洲精品国产| 亚洲综合精品香蕉久久网| 桃子视频在线观看高清免费视频| 亚洲18在线天美| 亚洲精品成人在线| 亚洲一区二区三区免费在线观看| 亚洲第一第二第三第四第五第六| 国产亚洲精品看片在线观看| 无码精品人妻一区二区三区免费看 | 天天操夜夜操免费视频| 国产精品免费久久久久久久久| 亚洲精品视频久久| 四虎永久免费地址在线观看| 免费观看久久精彩视频| 亚洲a∨无码精品色午夜| 亚洲av一综合av一区|