Fundamentals 9 min read

Understanding the Five Creational Design Patterns

This article provides a comprehensive overview of the five creational design patterns—Singleton, Abstract Factory, Factory Method, Builder, and Prototype—explaining their concepts, UML structures, implementation details, typical use cases, advantages, disadvantages, and includes Java code examples and diagrams.

Mike Chen's Internet Architecture
Mike Chen's Internet Architecture
Mike Chen's Internet Architecture
Understanding the Five Creational Design Patterns

Design patterns are essential for writing high‑quality software, closely related to architectural skills and source‑code reading. They help keep object‑oriented structures clean and serve as the foundation of software engineering.

Overview of Creational Patterns

Design patterns are divided into three categories: Creational, Structural, and Behavioral. This article focuses on the five creational patterns.

1. Singleton Pattern

The Singleton pattern ensures a class has only one instance and provides a global access point.

UML class diagram:

Key characteristics:

Only one instance of the class can exist.

The class creates its own unique instance.

The instance is provided to all other objects.

Eight common implementations (eager, lazy, double‑checked locking, etc.) are illustrated with diagrams.

2. Abstract Factory Pattern

Provides an interface for creating families of related objects without specifying concrete classes.

Structure diagram:

AbstractFactory : declares methods for creating abstract products.

ConcreteFactory : implements the abstract methods to create concrete products.

AbstractProduct : defines the interface for a type of product.

Product : concrete implementation of an abstract product.

Example scenario: Huawei and Xiaomi produce two product lines (elderly phones and student phones). Abstract Factory allows adding new product lines without changing client code.

3. Factory Method Pattern

Defines an interface for creating an object, but lets subclasses decide which class to instantiate, adhering to the Open‑Closed Principle.

UML diagram:

Product : abstract product class.

ConcreteProduct : concrete implementation of Product.

Factory : abstract creator that returns a Product.

ConcreteFactory : concrete creator that returns a ConcreteProduct.

Typical use case: replace Simple Factory when adding new product types would otherwise require modifying the factory class.

4. Builder Pattern

Separates the construction of a complex object from its representation, allowing the same construction process to create different representations.

UML class diagram:

Product : the complex object to be built.

Builder : abstract builder defining steps to create parts of the product.

ConcreteBuilder : implements Builder and assembles the product.

Director : orchestrates the building process (optional).

Typical Java example: @Override public StringBuilder append(CharSequence s) { super.append(s); return this; } (excerpt shown below).

@Override
public StringBuilder append(CharSequence s) {
    super.append(s); // implementation omitted
    return this;
}

5. Prototype Pattern

Creates new objects by cloning existing ones, avoiding the cost of repeated instantiation.

UML class diagram:

Client : uses the clone method of a concrete prototype.

Prototype : abstract class or interface declaring clone().

ConcretePrototype : implements clone() to produce a copy.

Example: cloning a business card using shallow or deep copy.

Conclusion

By studying this article, readers should understand the concepts, principles, typical scenarios, advantages, disadvantages, and implementation techniques of the five creational design patterns. Proper selection of patterns prevents misuse and improves code maintainability.

All content has been compiled into the PDF "深入浅出设计模式" (over 20,000 words, 50+ diagrams, 80+ pages) for convenient reference.

design patternsJavasoftware architectureUMLCreational Patterns
Mike Chen's Internet Architecture
Written by

Mike Chen's Internet Architecture

Over ten years of BAT architecture experience, shared generously!

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.