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

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

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

    e代劍客——溫柔一刀

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

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

    簡單工廠模式

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

    3. 一個簡單例子

    // 產(chǎn)品接口?
    public ? interface ?Product? {?

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

    }
    ?
    // 具體產(chǎn)品A?
    public ? class ?ProductA? implements ?Product? {?

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

    }
    ?
    // 具體產(chǎn)品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. 小結(jié)工廠模式的適用范圍
    ? 在編碼時不能預(yù)見需要創(chuàng)建哪一種類的實例。
    ? 一個類使用它的子類來創(chuàng)建對象。
    ? 開發(fā)人員不希望創(chuàng)建了哪個類的實例以及如何創(chuàng)建實例的信息暴露給外部程序。?


    ?

    ?

    抽象工廠模式

    ?

    1. 抽象工廠模式可以說是簡單工廠模式的擴展,它們主要的區(qū)別在于需要創(chuàng)建對象的復(fù)雜程度上。
    在抽象工廠模式中,抽象產(chǎn)品可能是一個或多個,從而構(gòu)成一個或多個產(chǎn)品族。 在只有一個產(chǎn)品族的情況下,抽象工廠模式實際上退化到工廠方法模式。
    2. 抽象工廠模式的結(jié)構(gòu)?

    3. 一個簡單例子

    // ?產(chǎn)品?Plant接口?
    public ? interface ?Plant? {?
    }
    ?
    // 具體產(chǎn)品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? " );?
    ??}
    ?
    }
    ?
    // ?產(chǎn)品?Fruit接口?
    public ? interface ?Fruit? {?
    }
    ?
    // 具體產(chǎn)品FruitA,F(xiàn)ruitB?
    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. 小結(jié)
    在以下情況下,應(yīng)當考慮使用抽象工廠模式。
      首先,一個系統(tǒng)應(yīng)當不依賴于產(chǎn)品類實例被創(chuàng)立,組成,和表示的細節(jié)。這對于所有形態(tài)的工廠模式都是重要的。
      其次,這個系統(tǒng)的產(chǎn)品有多于一個的產(chǎn)品族。
      第三,同屬于同一個產(chǎn)品族的產(chǎn)品是設(shè)計成在一起使用的。這一約束必須得在系統(tǒng)的設(shè)計中體現(xiàn)出來。
      最后,不同的產(chǎn)品以一系列的接口的面貌出現(xiàn),從而使系統(tǒng)不依賴于接口實現(xiàn)的細節(jié)。
      其中第二丶第三個條件是我們選用抽象工廠模式而非其它形態(tài)的工廠模式的關(guān)鍵性條件。

    posted on 2006-07-13 22:30 溫柔一刀 閱讀(259) 評論(0)  編輯  收藏 所屬分類: java相關(guān)
    聯(lián)系偶 zhupanjava@gmail.com 溫柔一刀
    主站蜘蛛池模板: 免费国产综合视频在线看| 亚洲综合一区二区国产精品| 国产一级一毛免费黄片| 久久亚洲精品成人无码网站| 免费无码不卡视频在线观看| a国产成人免费视频| 亚洲乱码在线观看| 久久久亚洲精品蜜桃臀| 大地资源二在线观看免费高清| 韩国免费三片在线视频| 国产做国产爱免费视频| 亚洲 日韩 色 图网站| 国产禁女女网站免费看| 国产精品免费高清在线观看| 欧美激情综合亚洲一二区| 亚洲国产精品久久久久婷婷老年| 中文字幕免费观看视频| 456亚洲人成在线播放网站| 亚洲人成色7777在线观看| 嫩草影院在线免费观看| 免费观看91视频| 日韩在线视频免费| 亚洲三级在线播放| 亚洲成av人影院| 亚洲高清国产拍精品青青草原 | 精品国产免费一区二区三区| 亚洲无限乱码一二三四区| 久久久久亚洲AV成人网人人网站| 又硬又粗又长又爽免费看 | 中文字幕不卡亚洲| 成全高清视频免费观看| 一级特黄aa毛片免费观看| 国产精品极品美女自在线观看免费 | 国产极品粉嫩泬免费观看| 16女性下面扒开无遮挡免费| 中文字幕免费播放| 一级女性全黄生活片免费看| 亚洲av永久无码精品网址| 亚洲国产综合在线| 亚洲国产精品线在线观看| 亚洲精品无码永久中文字幕|