Factory pattern is a creational design pattern that provides an interface for creating objects in a superclass but allows subclasses to alter the type of objects that will be created. It separates the responsibility of creating an object from the client that requests the object.
Here is an example of the factory pattern:
Let’s say we have a program that sells pizzas. We have different types of pizzas, such as cheese pizza, pepperoni pizza, and veggie pizza. Rather than creating individual classes for each type of pizza, we can use a factory pattern to create the pizzas.
public abstract class Pizza{
String name;
String dough;
String sauce;
ArrayList<String> toppings = new ArrayList<String>();
public void prepare(){
System.out.println("Preparing " + name);
System.out.println("Adding " + dough);
System.out.println("Adding " + sauce);
System.out.println("Adding toppings: ");
for(String topping : toppings){
System.out.println(" " + topping);
}
}
public void bake(){
System.out.println("Baking " + name);
}
public void cut(){
System.out.println("Cutting " + name);
}
public void box(){
System.out.println("Boxing " + name);
}
}
public class PizzaFactory{
public Pizza createPizza(String type){
Pizza pizza = null;
if(type.equals("cheese")){
pizza = new CheesePizza();
}
else if(type.equals("pepperoni")){
pizza = new PepperoniPizza();
}
else if(type.equals("veggie")){
pizza = new VeggiePizza();
}
return pizza;
}
}
public class CheesePizza extends Pizza{
public CheesePizza(){
name = "Cheese Pizza";
dough = "Regular Crust";
sauce = "Marinara Pizza Sauce";
toppings.add("Fresh Mozzarella");
toppings.add("Parmesan");
}
}
public class PepperoniPizza extends Pizza{
public PepperoniPizza(){
name = "Pepperoni Pizza";
dough = "Thick Crust";
sauce = "Tomato Sauce";
toppings.add("Pepperoni");
toppings.add("Sausage");
toppings.add("Mushrooms");
}
}
public class VeggiePizza extends Pizza{
public VeggiePizza(){
name = "Veggie Pizza";
dough = "Thin Crust";
sauce = "Pesto Sauce";
toppings.add("Red Peppers");
toppings.add("Green Peppers");
toppings.add("Mushrooms");
}
}
Now, whenever we want to create a pizza, we simply call the createPizza() method of the PizzaFactory class and pass in the type of pizza we want to create. For example:
PizzaFactory pizzaFactory = new PizzaFactory();
Pizza cheesePizza = pizzaFactory.createPizza("cheese");
cheesePizza.prepare();
cheesePizza.bake();
cheesePizza.cut();
cheesePizza.box();
Pizza veggiePizza = pizzaFactory.createPizza("veggie");
veggiePizza.prepare();
veggiePizza.bake();
veggiePizza.cut();
veggiePizza.box();
This will output:
Preparing Cheese Pizza
Adding Regular Crust
Adding Marinara Pizza Sauce
Adding toppings:
Fresh Mozzarella
Parmesan
Baking Cheese Pizza
Cutting Cheese Pizza
Boxing Cheese Pizza
Preparing Veggie Pizza
Adding Thin Crust
Adding Pesto Sauce
Adding toppings:
Red Peppers
Green Peppers
Mushrooms
Baking Veggie Pizza
Cutting Veggie Pizza
Boxing Veggie Pizza
Factory Pattern is a creational design pattern that provides an interface for creating objects in a superclass but allows subclasses to alter the type of objects being created.
It facilitates creating object instances without exposing the object creation logic.
It involves defining a separate method called the ‘Factory Method’ in the superclass that returns objects of the same ‘interface’ or ‘abstract class’.
The Factory Method is responsible for defining and creating the objects of the specific class that the Factory Pattern uses.
It helps in the separation of responsibilities between classes and improves code flexibility and extensibility.
It is an excellent pattern to use when you want to create objects that require a lot of computation or resources.
The Factory Pattern can be used with dependency injection, which resolves dependencies between different objects.
Some of the pros of using the Factory Pattern includes flexibility, maintainability, and scalability.
The Factory Pattern has some cons, including increased complexity, slower runtime, and difficulty in debugging.
There are different variations of the Factory Pattern including Simple Factory, Factory Method, Abstract Factory, Static Factory, and Dependency Injection.
What is the Factory Pattern?
Answer: Factory Pattern is a creational design pattern that provides an interface for creating objects of a class, but lets subclasses decide which class to instantiate.
What are the advantages of using the Factory Pattern?
Answer: Some of the advantages of using the Factory Pattern include improved code modularity, flexibility and extensibility, separation of concerns, enhanced code readability, and easier testing and debugging.
What is the difference between a Simple Factory and a Factory Method?
Answer: A Simple Factory creates objects of the same type, while a Factory Method creates objects of different types. Additionally, Simple Factory uses static methods, while Factory Method uses virtual methods.
What are some common use cases for the Factory Pattern?
Answer: Some common use cases for the Factory Pattern include object creation scenarios where object instantiation can be complex, such as when objects require a lot of configuration or initialization, or when there are different dependencies or versions of the object.
How does the Factory Pattern handle object creation and instantiation?
Answer: The Factory Pattern delegates object creation to its subclasses, which are responsible for instantiating and returning the appropriate object based on its specific logic and requirements. This allows for greater flexibility and abstraction in the code, as well as ease of maintenance and reuse.