A Factory Pattern or Factory Method Pattern says that just define an interface or abstract class for creating an object but let the subclasses decide which class to instantiate. In other words, subclasses are responsible to create the instance of the class.
public class PizzaStore{
Pizza orderPizza(){
Pizza pizza = new Pizza();
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
}
}
But when we need more than one type Pizza ? Then we have to modify our PizzaStore
public class PizzaStore{
Pizza orderPizza(String type){
Pizza pizza;
if(type.equalsIgnoreCase("cheese")){
pizza = new CheesePizza();
}else if(type.equalsIgnoreCase("pepperoni")){
pizza = new PepperoniPizza();
}else if(type.equalsIgnoreCase("calm")){
pizza = new CheesePizza();
}
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
}
}
Clearly, dealing with which concrete class is instantiated is really messing up our orderPizza() method and preventing it from being closed for modification. But now that we know what is varying and what isn’t, it’s probably time to encapsulate it.
public class SimplePizzaFactory {
public Pizza createPizza(String type){
Pizza pizza = null;
if(type.equalsIgnoreCase("cheese")){
pizza = new CheesePizza();
}else if(type.equalsIgnoreCase("pepperoni"))
{
pizza = new PepperoniPizza();
}else if(type.equalsIgnoreCase("calm")){
pizza = new CheesePizza();
}
return pizza;
}
}
public class PizzaStore {
SimplePizzaFactory factory;
public PizzaStore(SimplePizzaFactory factory){
this.factory = factory;
}
public Pizza orderPizza(String type)
{
Pizza pizza;
pizza = factory.createPizza(type);
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
return pizza;
}
}
public class Demo {
public static void main(String[] args) {
SimplePizzaFactory factory = new SimplePizzaFactory();
PizzaStore pizzaStore = new PizzaStore(factory);
pizzaStore.orderPizza("cheese");
}
}
Here Pizza is an Interface CheesePizza, CalmPizza and PepperoniPizza implemented this Pizza interface. (See code for better underesting)