简单工厂将复杂的创建过程集中到一个简单工厂类中
类图
示例
普通写法:
public interface Thing {}
public class ConcreteThingA implements Thing {}
public class ConcreteThingB implements Thing {}
Thing thing;
if (xxx) {
thing = new ConcreteThingA();
} else if (xxx) {
thing = new ConcreteThingB();
}
...
简单工厂写法:
public class SimpleFactory {
public static Thing create(XXX xxx) {
if (xxx) {
return new ConcreteThingA();
} else if (xxx) {
return new ConcreteThingB();
}
return new ConcreteThingXXX();
}
}
使用
Thing thing = SimpleFactory.create(xxx);