Architecture

Creational Design Patterns: Building Objects with Flexibility

Master the art of flexible object creation—discover how Factory, Builder, Singleton, and Prototype patterns solve the fundamental challenge of building objects that are decoupled, testable, and adaptable to change

Series: Design Patterns and Analysis | Part 2 of 4 > Developed during Master’s in Web Systems Projects

Continuing our series, after understanding the importance of design patterns, we now explore the first specific category. Creational patterns solve one of the most fundamental problems in object-oriented design.

Creational patterns solve one of the most fundamental problems in object-oriented design: How do we create objects in a way that is flexible, decoupled, and testable?

This post dives deeper into the first category of design patterns — Creational Patterns — and explores their key ideas, structure, and when to use them in real systems.

What Are Creational Patterns?

These patterns encapsulate the object creation process, hiding the complexities of instantiation and making the code more flexible to change.

They help avoid:

  • Hardcoding constructors (new) all over the system
  • Tightly coupling your code to specific classes
  • Problems in object configuration, duplication, or reuse

Types of Creational Patterns

Factory Method

Creates objects through a factory interface instead of calling constructors directly.

  • Intent: Define an interface for creating an object, but let subclasses decide which class to instantiate.
  • Use When: You want to delegate instantiation to subclasses.
UML Diagram of Factory Method
abstract class Dialog {
    public void renderWindow() {
        Button okButton = createButton();
        okButton.render();
    }
    protected abstract Button createButton();
}

Abstract Factory

Groups related factories together under a unified interface.

  • Intent: Provide an interface for creating families of related or dependent objects without specifying their concrete classes.
  • Use When: You want to ensure consistent object creation across multiple variants (e.g., LightThemeButton + LightThemeInput).
UML Diagram of Abstract Factory
interface GUIFactory {
    Button createButton();
    Checkbox createCheckbox();
}

Builder

Separates the construction of a complex object from its representation.

  • Intent: Construct objects step-by-step using the same process, with different representations.
  • Use When: You need to create objects with many configuration options or steps.
UML Diagram of Builder
class CarBuilder {
    CarBuilder setSeats(int count);
    CarBuilder setEngine(Engine engine);
    Car build();
}

Prototype

Clones existing objects instead of creating new ones from scratch.

  • Intent: Specify the kinds of objects to create using a prototypical instance and clone it.
  • Use When: Instantiation is costly, or object configuration is complex.
UML Diagram of Prototype
abstract class Shape {
    public Shape clone() {
        return (Shape) this.clone();
    }
}

Singleton

Ensures only one instance of a class exists and provides global access to it.

  • Intent: Control instantiation and provide one shared object.
  • Use With Caution: It introduces global state and can hinder testability.
UML Diagram of Singleton
class Config {
    private static Config instance;
    private Config() {}

    public static Config getInstance() {
        if (instance == null) {
            instance = new Config();
        }
        return instance;
    }
}

Comparison Table

PatternResponsibilityBest For
Factory MethodDelegates creation to subclassesFrameworks, plugin systems
Abstract FactoryGroup creation of related productsThemed UI kits, database connectors
BuilderStep-by-step constructionComplex configuration, fluent APIs
PrototypeClone existing instancePerformance optimization, object trees
SingletonUnique shared instanceLogging, configuration, caches

Final Thoughts

Creational patterns shape the starting point of any object in your system. They may look simple, but they determine how flexible, testable, and reusable your entire architecture will be.

Next up, we’ll explore Structural Patterns — and how to compose objects into larger systems cleanly.


Series Navigation