The Simple Factory isn't actually a Design Pattern; it's more of a programming idiom. It is very commonly used, however, so it is included here. The purpose is to move extensive creation work outside of a class and into another, so that only the Simple Factory class need be changed if a new version must be created, leaving the original class intact.
An example of the Simple Factory Design Pattern follows:
public class SimplePizzaFactory {
public Pizza createPizza(String type) {
Pizza pizza;
if (type.equals("cheese")) {
pizza = new CheesePizza();
}
if (type.equals("pepperoni")) {
pizza = new PepperoniPizza();
}
if (type.equals("veggie")) {
pizza = new VeggiePizza():
}
return pizza;
}
}
public class PizzaStore {
SimplePizzaFactory factory;
public PizzaStore(SimplePizzaFactory factory) {
this.factory = factory;
}
public Pizza orderPizza(String ttype) {
Pizza pizza;
pizza = factory.createPizza(type);
pizza.prepare();
pizza.bake();
...
}
}