构建过程和调用层相分离,适用于产品族比较多。
代码示例:
public interface IFactory{
IProduct create();
}
public class Factory1 implements IFactory{
@Override
public IProduct create() {
return new Product1();
}
}
public class Factory2 implements IFactory{
@Override
public IProduct create() {
return new Product2();
}
}
public interface IProduct{
void doSomething();
}
public class Product1 implements IProduct{
@Override
public void doSomething() {
Log.d(TAG, "doSomething: 1");
}
}
public class Product2 implements IProduct{
@Override
public void doSomething() {
Log.d(TAG, "doSomething: 2");
}
}
public class Test{
public static void main(String[] args){
IFactory factory = new Factory1();
factory.create.doSomething();
}
}