package abstractFactory;
public interface Creator {
/*
* 产品等级结构A的工厂方法
*/
public ProductA factoryA();
/*
* 产品等级结构B的工厂方法
*/
public ProductB factoryB();
}
package abstractFactory;
public class ConcreteCreator1 implements Creator {
/*
* 产品等级结构A的工厂方法
*/
public ProductA factoryA() {
return new ProductA1();
}
/*
* 产品等级结构B的工厂方法
*/
public ProductB factoryB() {
return new ProductB1();
}
}
package abstractFactory;
public class ConcreteCreator2 implements Creator {
/*
* 产品等级结构A的工厂方法
*/
public ProductA factoryA() {
return new ProductA2();
}
/*
* 产品等级结构B的工厂方法
*/
public ProductB factoryB() {
return new ProductB2();
}
}
package abstractFactory;
public interface ProductA {
}
package abstractFactory;
import static logger.Log.debug;
public class ProductA1 implements ProductA {
/*
* 构造子
*/
public ProductA1(){
debug("ProductA1");
}
}
package abstractFactory;
import static logger.Log.debug;
public class ProductA2 implements ProductA {
/*
* 构造子
*/
public ProductA2() {
debug("ProductA2");
}
}
package abstractFactory;
public interface ProductB {
}
package abstractFactory;
import static logger.Log.debug;
public class ProductB1 implements ProductB {
/*
* 构造子
*/
public ProductB1(){
debug("ProductB1");
}
}
package abstractFactory;
import static logger.Log.debug;
public class ProductB2 implements ProductB {
/*
* 构造子
*/
public ProductB2(){
debug("ProductB2");
}
}
package abstractFactory;
/*
* 客户端测试
*/
public class Client {
/*
* 工厂
*/
private static Creator creator1,creator2;
/*
* A等级产品
*/
@SuppressWarnings("unused")
private static ProductA producta1,producta2;
/*
* B等级产品
*/
@SuppressWarnings("unused")
private static ProductB productb1,productb2;
public static void main(String[] args){
creator1 = new ConcreteCreator1();
creator2 = new ConcreteCreator2();
/*
* 产品族1
*/
producta1 = creator1.factoryA();
productb1 = creator1.factoryB();
/*
* 产品族2
*/
producta2 = creator2.factoryA();
productb2 = creator2.factoryB();
}
}