posts - 2, comments - 2, trackbacks - 0, articles - 23
            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理
          一、抽象工廠模式和工廠方法模式的區(qū)別是:
                  工廠方法模式是一個工廠具體類對應(yīng)一個產(chǎn)品的等級結(jié)構(gòu),而Abstract Factory 是一個工廠的具體類相應(yīng)多個產(chǎn)品的等級結(jié)構(gòu)
          二、UML圖

                                      
          三:簡單測試代碼如下:
           1/**
           2 * 抽象的工廠接口
           3 */

           4public interface IFactory {
           5    /**
           6     * 等級結(jié)構(gòu)A的工廠方法
           7     */

           8    public IProductA factoryA();
           9    
          10    /**
          11     * 等級結(jié)構(gòu)B的工廠方法
          12     */

          13    public IProductB factoryB();
          14}

          15public class FactoryA implements IFactory {
          16
          17    public IProductA factoryA() {
          18        return new ProductA();
          19    }

          20
          21    public IProductB factoryB() {
          22        return new ProductB();
          23    }

          24
          25}

          26public class FactoryB implements IFactory {
          27
          28    public IProductA factoryA() {
          29        return new ProductA();
          30    }

          31
          32    public IProductB factoryB() {
          33        return new ProductB();
          34    }

          35
          36}

          37/**
          38 * 產(chǎn)品的接口
          39 */

          40
          41public interface IProductA {
          42    void helloA();
          43}

          44
          45public class ProductA implements IProductA{
          46
          47    public void helloA() {
          48        System.out.println("ProductA");
          49    }

          50
          51}

          52
          53public interface IProductB {
          54    void helloB();
          55}

          56public class ProductB implements IProductB{
          57
          58    public void helloB() {
          59        System.out.println("Product B");
          60    }

          61}

          62
          63public class TestAbstractFactory {
          64    
          65    /**
          66     * 測試抽象工廠模式
          67     * @param args
          68     */

          69    public static void main(String[] args) {
          70        IFactory f = new FactoryA();
          71        f.factoryA().helloA();
          72        f.factoryB().helloB();
          73        
          74        IFactory f1 = new FactoryB();
          75        f1.factoryA().helloA();
          76        f1.factoryB().helloB();
          77    }

          78
          79}

          80
          81
          82
          主站蜘蛛池模板: 景洪市| 扬州市| 牟定县| 讷河市| 家居| 乌海市| 马尔康县| 南城县| 弥勒县| 镇坪县| 苏尼特右旗| 郸城县| 尼玛县| 贺兰县| 张家港市| 抚州市| 灵川县| 封开县| 丰顺县| 内乡县| 嘉兴市| 宜兰市| 六盘水市| 永兴县| 沈阳市| 延长县| 浙江省| 思南县| 宁化县| 凤翔县| 额尔古纳市| 曲阳县| 巴马| 城固县| 江阴市| 宁国市| 沾益县| 崇信县| 阿拉善左旗| 阜宁县| 常熟市|