现在的位置: 首页 > 综合 > 正文

★ 对抽象工厂模式的一点理解

2013年12月12日 ⁄ 综合 ⁄ 共 1245字 ⁄ 字号 评论关闭
使用Aosu易博通,一分钱不花,实现网文自动摘抄, 博客写作方便又快捷,和您现在看到的一样 !自主嵌入Google广告,还能赚取美金! 现在就下载

 
package abstractfactory;

// 接口  A
interface ProductA {
    void sayHello(String name);
}

//  类的具体的实现 一
class ProductA1 implements ProductA {
    public void sayHello(String name) {
        System.out.println("hello : " + name);
    }
}

//  类的实现 二
class ProductA2 implements ProductA {
    public void sayHello(String name) {
        System.out.println("hello : " + name);
    }
}

//接口 B
interface ProductB {
       ...........
}

// 类的具体的实现一
class ProductB1 implements ProductB {
       ..........
}

// 类的具体的实现二
class ProductB2 implements ProductB {
       ...........
}

//   返回对象实例 的接口
interface Creator {
    public ProductA factoryA();

    public ProductB factoryB();
}

//  抽象工厂的接口具体的实现 一
class Creator1 implements Creator {
    public ProductA factoryA() {
        return new ProductA1();
    }

    public ProductB factoryB() {
        return new ProductB1();
    }

}

//  抽象工厂的具体实现 二
class Creator2 implements Creator {
    public ProductA factoryA() {
        return new ProductA2();
    }

    public ProductB factoryB() {
        return new ProductB2();
    }

}

//   测试方法
public class Test {

    public static void main(String[] args) {

        ProductA pa;
        ProductB pb;

        Creator1 creator1 = new Creator1();
        pa = creator1.factoryA();
        pa.sayHello("张迪");

    }

}

 
今日热点:
  • ASP.NET2.0中全面实现文件图片上传下载处理
  • ASP.NET2.0 文本编辑器FCKeditor
  • Eclipse的Adapter机制
  •  
     
     

    抱歉!评论已关闭.