tech-docs

Living documentation for evolving technologies

View on GitHub

Simple Factory


Table of Contents


The Simple Factory is a programming idiom — not a formal GoF pattern — that centralises object creation in a single static method using a conditional dispatch. It trades the Open/Closed Principle for simplicity and is a common first step before adopting the formal Factory Method pattern.


Overview

Without any factory, every caller that needs a Shape must decide which concrete class to instantiate and call new Circle() or new Rectangle() directly. This scatters creation logic across the codebase and couples every caller to concrete types.

Simple Factory extracts that conditional into one class. All callers depend only on the factory and the Shape interface. When the set of supported types is small and stable, this is a pragmatic solution that avoids the ceremony of subclassing.

The trade-off is OCP compliance: every time a new product type is added, the factory class must be modified. When types change frequently, Simple Factory is a signal to evolve to Factory Method.

Back to top


Participants

The Simple Factory pattern involves four elements.

Back to top


Structure

classDiagram
    class Shape {
        <<interface>>
        +draw() void
    }
    class Circle {
        +draw() void
    }
    class Rectangle {
        +draw() void
    }
    class ShapeFactory {
        +create(type String)$ Shape
    }
    class Client

    Shape <|.. Circle
    Shape <|.. Rectangle
    Client --> ShapeFactory : calls create()
    ShapeFactory ..> Shape : creates

Caption: The Simple Factory centralises object creation in a single static method. The client depends only on the factory and the Shape interface, never on concrete classes — but every new type requires modifying ShapeFactory.

Back to top


Example

The following Java example shows a ShapeFactory using a switch expression to map a string key to a concrete product.

public interface Shape {
    void draw();
}

public class Circle    implements Shape { public void draw() { /* ... */ } }
public class Rectangle implements Shape { public void draw() { /* ... */ } }

public class ShapeFactory {
    public static Shape create(String type) {
        return switch (type) {
            case "circle"    -> new Circle();
            case "rectangle" -> new Rectangle();
            default          -> throw new IllegalArgumentException("Unknown shape: " + type);
        };
    }
}

// Client code — no reference to Circle or Rectangle
Shape s = ShapeFactory.create("circle");
s.draw();

The client receives a Shape reference. It has no compile-time dependency on Circle or Rectangle.

Back to top


When to Use

Use Simple Factory when:

Do not use Simple Factory when:

Back to top


Q&A

Common questions a software architect trainee would ask about this topic.

Q: If Simple Factory is not a GoF pattern, why is it worth learning? A: It is the most common introduction to the concept of decoupling creation from use. Understanding its limitations — specifically that every new type requires modifying the factory class — is precisely what motivates learning the formal GoF patterns. Most production codebases contain Simple Factories, so recognising them is a practical skill.


Q: Does Simple Factory violate the Open/Closed Principle? A: Yes. Every time a new product type is needed, the create() method must be modified. This is the primary design limitation of the idiom. Factory Method resolves this by replacing the conditional with polymorphism — a new product type requires only a new subclass, not a modification to existing code.


Q: Can Simple Factory be non-static? A: Yes. A non-static factory can hold configuration or state that influences which product to create. However, the structural limitation — a single class containing all creation decisions — remains the same regardless of whether the method is static.

Back to top


Back to top


Ref.


Get Started Factory Patterns