Factory pattern has 2 types, simple factory and general factory patterns.
Simple factory pattern:
Factory class helps create new objects, whenever needs to create a new object, ask factory class to return.
Advantages: Simple, easy to apply, only factory class needs adjustments when new type of objects is involved. Widely used.
Disadvantage: Doesn't meet open and close principle, when extends new types of objects, need to update factory class.
UML class diagram:
Pizza pizza = PizzaSimpleFactory.createPizza(pizzaType);
General factory pattern:
Each type of object will have own combination of object class and factory class, when new object is required, will create the new object and new object's factory classes.
Advantages: Meet open and close principle
Disadvantages: Too many classes and code
UML class diagram:
String pizzaType = pizzaTypeStream.readLine();
Pizza pizza = null;
if (location.equals("Bei Jing")) {
pizza = new BJPizzaFactory().createPizza(pizzaType);
} else if (location.equals("Tian Jin")) {
pizza = new TJPizzaFactory().createPizza(pizzaType);
} else {
System.out.println("The location you choose is not supported");
return;
}