Factory Design Pattern: Examples in Java

Observe the code below:

        ShapeFactory factory = new ShapeFactory();

        Shape shape1 = factory.getShape("circle");
        Shape shape2 = factory.getShape("square");
        Shape shape3 = factory.getShape("rectangle");

        shape1.draw();
        shape2.draw();
        shape3.draw();

We wanted to create objects such as shape1, shape2, and shape 3. We did not directly (create and) call the class for the required objects. We did not create or call a circle class, or a square class, or a rectangle class. We rather thought there was a factory class that could create objects based on our requirements. We asked the factory to create a circle object, then it gave us a circle object. In the same way, we asked for square and rectangle objects from the factory class, and the class returned us square and rectangular objects.

This is about: Factory Design Pattern: Examples are in Java

From AI Tools/OpenAI/Internet

Perfect 👍 — here’s a copyright-free write-up of the Factory Pattern in Java that matches the same style as the Builder Pattern post, so you can share both on Facebook or your blog.


⚙️ Java Design Patterns – Factory Pattern

🔹 Definition

The Factory Pattern is a creational design pattern that provides a way to create objects without exposing the creation logic to the client. Instead of directly calling a constructor, you use a factory method that decides which object to return.


🔹 Use Cases

You should use the Factory Pattern when:

  • You want to delegate object creation to a central place.
  • The exact type of object needed is determined at runtime.
  • You want to reduce tight coupling between classes.
  • Multiple subclasses share a common interface but have different implementations.

🔹 Advantages

✔️ Encapsulation of object creation logic.
✔️ Makes code easier to extend when adding new object types.
✔️ Promotes loose coupling (client depends on interface, not implementation).
✔️ Simplifies maintenance and testing.
✔️ Encourages code reuse.


🔹 Example in Java

// Product Interface
interface Shape {
    void draw();
}

// Concrete Products
class Circle implements Shape {
    public void draw() {
        System.out.println("Drawing a Circle.");
    }
}

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

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

// Factory Class
class ShapeFactory {
    public Shape getShape(String shapeType) {
        if (shapeType == null) return null;
        switch (shapeType.toLowerCase()) {
            case "circle": return new Circle();
            case "square": return new Square();
            case "rectangle": return new Rectangle();
            default: return null;
        }
    }
}

// Usage
public class FactoryPatternExample {
    public static void main(String[] args) {
        ShapeFactory factory = new ShapeFactory();

        Shape shape1 = factory.getShape("circle");
        Shape shape2 = factory.getShape("square");
        Shape shape3 = factory.getShape("rectangle");

        shape1.draw();
        shape2.draw();
        shape3.draw();
    }
}

Output:

Drawing a Circle.
Drawing a Square.
Drawing a Rectangle.

📌 In short:
The Factory Pattern lets you create objects without knowing their exact class. It simplifies code, promotes loose coupling, and makes it easier to manage different object types from a single point.


👉 Now that we have both Builder and Factory, would you like me to also prepare a comparison table (Builder vs Factory) in the same copyright-free format for FB/Blog? That way your readers can instantly see the differences.

Leave a Reply