DEV Community

Cover image for Factory Method in Java
Matheus Bernardes Spilari
Matheus Bernardes Spilari

Posted on

Factory Method in Java

The Factory Method 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 helps achieve loose coupling and promotes the Open-Closed Principle (OCP) in object-oriented design.

In this post, we'll explore the Factory Method pattern using a fun and easy-to-understand example: creating different types of drinks! β˜•πŸ₯€


Why Use the Factory Method?

βœ… Encapsulation – The creation logic is hidden from the client.

βœ… Flexibility – New types of objects can be added without modifying existing code.

βœ… Loose Coupling – The client code depends only on an abstract interface, not concrete classes.


Example: A Drink Factory πŸ₯€

Imagine we have a drink shop that serves Tea, Coffee, and Soda. Instead of manually creating each drink in different parts of the code, we'll use the Factory Method to centralize and simplify object creation.

Step 1: Define an Interface for Drinks

We create a common interface Drink that all drink types will implement.

public interface Drink {
    void prepare();
}
Enter fullscreen mode Exit fullscreen mode

Step 2: Create Concrete Drink Classes

Each drink class implements the Drink interface and defines its own preparation method.

public class Tea implements Drink {
    @Override
    public void prepare() {
        System.out.println("Boiling water, adding tea leaves, and serving tea.");
    }
}

public class Coffee implements Drink {
    @Override
    public void prepare() {
        System.out.println("Brewing coffee, adding sugar and milk, and serving coffee.");
    }
}

public class Soda implements Drink {
    @Override
    public void prepare() {
        System.out.println("Pouring soda into a glass and adding ice.");
    }
}
Enter fullscreen mode Exit fullscreen mode

Step 3: Create the Factory Method

Now, we implement a DrinkFactory that will be responsible for creating drink objects based on a given type.

public class DrinkFactory {
    public static Drink createDrink(String type) {
        switch (type.toLowerCase()) {
            case "tea":
                return new Tea();
            case "coffee":
                return new Coffee();
            case "soda":
                return new Soda();
            default:
                throw new IllegalArgumentException("Unknown drink type: " + type);
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή Note: We used a switch statement for simplicity, but a more scalable approach would be implementing the Strategy Pattern instead.


Step 4: Using the Factory in the Main Program

Now, let's see how we can use our DrinkFactory to create drinks dynamically.

public class FactoryMethodExample {
    public static void main(String[] args) {
        Drink tea = DrinkFactory.createDrink("tea");
        Drink coffee = DrinkFactory.createDrink("coffee");
        Drink soda = DrinkFactory.createDrink("soda");

        tea.prepare();
        coffee.prepare();
        soda.prepare();
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

Boiling water, adding tea leaves, and serving tea.
Brewing coffee, adding sugar and milk, and serving coffee.
Pouring soda into a glass and adding ice.
Enter fullscreen mode Exit fullscreen mode

Pros and Cons of the Factory Method Pattern

βœ… Pros:

βœ” Encapsulation – The object creation logic is hidden from the client.

βœ” Scalability – New drink types can be added without changing client code.

βœ” Code Reusability – The factory centralizes object creation, making maintenance easier.

❌ Cons:

βœ– More Classes and Complexity – Introducing a factory adds extra classes.

βœ– Difficult to Understand at First – Beginners might find it harder to follow compared to simple object instantiation (new Tea()).


Common Use Cases for Factory Method

The Factory Method is widely used in real-world applications:

βœ” GUI Frameworks (e.g., creating different button types)

βœ” Database Connections (e.g., connecting to MySQL, PostgreSQL, etc.)

βœ” Logging Systems (e.g., different log levels or outputs)

βœ” Parsing Documents (e.g., handling JSON, XML, etc.)


Conclusion

The Factory Method is a powerful pattern that improves code maintainability and flexibility. While we used a switch statement for simplicity, a better approach for scalability would be implementing the Strategy Pattern to handle different drink types dynamically.


πŸ“ Reference

πŸ’» Project Repository

πŸ‘‹ Talk to me

Top comments (0)