WIKI:http://zh.wikipedia.org/wiki/%E6%8A%BD%E8%B1%A1%E5%B7%A5%E5%8E%82%E6%A8%A1%E5%BC%8F
abstract class AbstractProductA{
public abstract void operationA1();
public abstract void operationA2();
}
class ProductA1 extends AbstractProductA{
ProductA1(String arg){
System.out.println("ProductA1: Hello "+arg);
}
public void operationA1() { };
public void operationA2() { };
}
class ProductA2 extends AbstractProductA{
ProductA2(String arg){
System.out.println("ProductA2: Hello "+arg);
}
public void operationA1() { };
public void operationA2() { };
}
abstract class AbstractProductB{
//public abstract void operationB1();
//public abstract void operationB2();
}
class ProductB1 extends AbstractProductB{
ProductB1(String arg){
System.out.println("ProductB1: Hello "+arg);
}
}
class ProductB2 extends AbstractProductB{
ProductB2(String arg){
System.out.println("ProductB2: Hello "+arg);
}
}
abstract class AbstractFactory{
abstract AbstractProductA createProductA();
abstract AbstractProductB createProductB();
}
class ConcreteFactory1 extends AbstractFactory{
AbstractProductA createProductA(){
return new ProductA1("ConcreteFactory1");
}
AbstractProductB createProductB(){
return new ProductB1("ConcreteFactory1");
}
}
class ConcreteFactory2 extends AbstractFactory{
AbstractProductA createProductA(){
return new ProductA2("ConcreteFactory2");
}
AbstractProductB createProductB(){
return new ProductB2("ConcreteFactory2");
}
}
public class AbFactoryExample{
public static void main(String args[]){
AbstractFactory pf1 = new ConcreteFactory1();
AbstractProductA product1 = pf1.createProductA();
AbstractFactory pf2 = new ConcreteFactory2();
AbstractProductA product2 = pf2.createProductA();
}
}
“工厂”是创建产品(对象)的地方,其目的是将产品的创建与产品的使用分离。抽象工厂模式的目的,是将若干抽象产品的接口与不同主题产品的具体实现分离开。