00:00

Factory Design Pattern

The Factory Design Pattern is a creational design pattern. It is used when we want to create objects without exposing the object creation logic to the client. Instead of creating objects using the new keyword directly, we use a factory method to get the required object.

Why Do We Need the Factory Design Pattern?

In real-world applications, object creation can become complex and repetitive. If we create objects directly using new, the code becomes tightly coupled and hard to maintain.

The Factory Pattern helps to:

  • Hide object creation logic
  • Reduce tight coupling between classes
  • Improve code readability and maintainability
  • Make the application easier to extend in the future

Real-Life Example

Think of a vehicle factory. You don’t ask the factory how to build a car or a bike. You just say, “Give me a car” or “Give me a bike,” and the factory gives it to you.

Similarly, in software, the Factory Pattern creates objects for us based on the given input.

Factory Design Pattern Structure

  • Product Interface – Common interface for all objects
  • Concrete Classes – Actual implementations
  • Factory Class – Contains logic to create objects

Factory Design Pattern Example in Java

Step 1: Create an Interface


public interface Shape {
    void draw();
}

Step 2: Create Concrete Classes


public class Circle implements Shape {
    @Override
    public void draw() {
        System.out.println("Drawing a Circle");
    }
}

public class Rectangle implements Shape {
    @Override
    public void draw() {
        System.out.println("Drawing a Rectangle");
    }
}

Step 3: Create the Factory Class


public class ShapeFactory {

    public static Shape getShape(String shapeType) {

        if (shapeType == null) {
            return null;
        }

        if (shapeType.equalsIgnoreCase("CIRCLE")) {
            return new Circle();
        } 
        else if (shapeType.equalsIgnoreCase("RECTANGLE")) {
            return new Rectangle();
        }

        return null;
    }
}

Step 4: Use the Factory in Client Code


public class FactoryPatternDemo {

    public static void main(String[] args) {

        Shape shape1 = ShapeFactory.getShape("CIRCLE");
        shape1.draw();

        Shape shape2 = ShapeFactory.getShape("RECTANGLE");
        shape2.draw();
    }
}

Output


Drawing a Circle
Drawing a Rectangle

Key Benefits of Factory Design Pattern

  • Client code does not depend on concrete classes
  • Centralized object creation
  • Easier to add new types without modifying existing client code
  • Follows the Open/Closed Principle

When Should You Use the Factory Pattern?

  • When object creation logic is complex
  • When you want to hide implementation details
  • When the exact type of object is decided at runtime

Conclusion

The Factory Design Pattern is a powerful and commonly used pattern in Java applications. It helps in writing clean, flexible, and maintainable code by separating object creation from business logic.

If you are preparing for Java interviews or building scalable applications, understanding this pattern is a must.