Loading
15, Jan 2025

Understanding the Factory Design Pattern

Let’s start with one of the most popular and beginner-friendly design patterns the Factory Design Pattern. 

The Factory Design Pattern is a creational design pattern whose main objective is to simplify the creation of objects. Instead of creating objects directly in your code, let the factory pattern create them for you. 

Let us understand with a simple real-world example. If you go to a restaurant to have a pizza, you just place an order for the pizza you want and let the restaurant handle the entire making process. In the end, you just enjoy the pizza. The same goes for the factory design pattern, if you need an object let the factory pattern make the object for you. You just call the factory pattern to get your object.

It may be simple to understand, but our goal here is to design the factory itself. A factory can create multiple objects of similar types. The output of the factory is dependent on the input provided. The same concept applies to the pizza restaurant – different customers prefer different types of pizza. It is the responsibility of the restaurant to deliver those varieties of pizza to different customers.

 


 

Now let’s understand how to implement it using Java

 

Java provides an interface for creating objects in a superclass while allowing its subclasses to specify the types of objects they create. Here’s how it works:

  • Firstly, the interface is used to define the blueprint or a contract that all the concrete implementations must follow.
  • Then the subclasses implement the interface and provide specific behavior for each type of object.
  • Finally, the factory class takes the input that determines which subclass to instantiate and return the object.

 

Let’s visualize it

 

Factory Design Pattern
Fig1: visualization of factory design pattern

 

  1. The Pizza Class
    Acts as a blueprint for objects that the factory will create.

    public interface Pizza {
        void create();
    }
    
  2. Subclasses (CheesePizza, MixPizza and VegPizza)
    Different types (implementations) of the product/pizza.

    public class CheesePizza implements Pizza{
        @Override
        public void create() {
            System.out.println("Creating Cheese Pizza");
        }
    }
    public class VegPizza implements Pizza{
        @Override
        public void create() {
            System.out.println("Creating Veg Pizza");
        }
    }
    
    public class MixPizza implements Pizza{
        @Override
        public void create() {
            System.out.println("Creating Mix Pizza");
        }
    }
    
  3. PizzaFactory (The Factory)
    The class that creates the products/pizza.

    public class PizzaFactory {
        public Pizza getPizza(String type){
            switch (type){
                case "cheese":
                    return new CheesePizza();
                case "mix":
                    return new MixPizza();
                case "veg":
                    return new VegPizza();
                default:
                    return null;
            }
        }
    }

Finally, lets create a main class i.e, our Restaurant that calls the PizzaFactory to get a Pizza based on customer choice.

public class Restaurant {
    public static void main(String[] args) {
        PizzaFactory pizzaFactory = new PizzaFactory();

        Pizza cheesePizza = pizzaFactory.getPizza("cheese");
        cheesePizza.create();

        Pizza vegPizza = pizzaFactory.getPizza("veg");
        vegPizza.create();

        Pizza mixPizza = pizzaFactory.getPizza("mix");
        mixPizza.create();
    }
}

Which gives us the output

Creating Cheese Pizza
Creating Veg Pizza
Creating Mix Pizza

 


 

So, why should you use Factory Design Pattern?

 

Now you know what the factory design pattern is and how to implement it. But now the question is why use it?

  1. Hides Complexities
    It hides the complexities behind the object creations that are non-essential for the client.
  2. Flexibility
    Now you have complete flexibility to add new types without modifying the existing code. Just simply add a new class and implement the interface.
  3. Neat and Clean
    Once the object creation logic has been centralized the code becomes cleaner and easy to understand.
  4. Makes life easier
    Your life becomes easier when it’s easier to maintain and extend over time. What else do you need?

 


 

When should you use it?

 

The answer is of course not every time. It depends on the situation.

  1. When you don’t want the client code to worry about how objects are created.
  2. When your application needs to be flexible and support new types of objects in the future.
  3. When you want to centralize object creation logic for better maintainability.

 


 

Let’s wrap up

 

The Factory Design Pattern keeps your code clean, flexible, and easy to work with. By abstracting the object creation logic, it lets you focus on what really matters—building awesome applications.
Next time you’re working on a Java project, think about whether the Factory Pattern can help you. It’s simple to implement, and as you’ve seen, it can make your code much easier to manage.

 


Happy Coding!

Leave a Reply

Your email address will not be published. Required fields are marked *