抽象工厂模式其实是一种最一般的工厂模式形态,简单工厂模式和工厂方法模式都可以看成抽象工厂模式的特殊实现.
抽象工厂模式提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类
import java.util.*;
interface Ifunc1 {
public void work1_1();
public void work1_2();
}
interface Ifunc2 {
public void work2_1();
public void work2_2();
}
class ProductA1 implements Ifunc1 {
public void work1_1() {
System.out.println("ProductA1 work1_1");
}
public void work1_2() {
System.out.println("ProductA1 work1_2");
}
}
class ProductB1 implements Ifunc1 {
public void work1_1() {
System.out.println("ProductB1 work1_1");
}
public void work1_2() {
System.out.println("ProductB1 work1_2");
}
}
class ProductA2 implements Ifunc2 {
public void work2_1() {
System.out.println("ProductA2 work2_1");
}
public void work2_2() {
System.out.println("ProductA2 work2_2");
}
}
class ProductB2 implements Ifunc2 {
public void work2_1() {
System.out.println("ProductB2 work2_1");
}
public void work2_2() {
System.out.println("ProductB2 work2_2");
}
}
interface IFactory {
public Ifunc1 createIfunc1();
public Ifunc2 createIfunc2();
}
class ProductAFactory implements IFactory {
public Ifunc1 createIfunc1() {
return new ProductA1();
}
public Ifunc2 createIfunc2() {
return new ProductA2();
}
}
class ProductBFactory implements IFactory {
public Ifunc1 createIfunc1() {
return new ProductB1();
}
public Ifunc2 createIfunc2() {
return new ProductB2();
}
}
public class AbstractFactoryMethod {
public static void main(String[] args) {
IFactory factoryA = new ProductAFactory();
factoryA.createIfunc1().work1_1();
factoryA.createIfunc1().work1_2();
}
}