Creational Design Patterns

Patterns for object creation: Singleton, Factory Method, Abstract Factory, Builder, and Prototype with intents and use cases.

  • JSON4,045 B
  • YAML2,849 B · −30%
  • TOON2,399 B · −41%

Creational design patterns that control how objects are created: Singleton, Factory Method, Abstract Factory, Builder and Prototype, plus Object Pool, Lazy Initialization and Dependency Injection, each with intent and when to use it.

Use it when

  • You need to decouple code from the concrete classes it instantiates.
  • You are choosing among factory-style patterns.

Watch out for

  • Singleton is often overused and acts as hidden global state; dependency injection is usually a better alternative.
  • Dependency injection is a technique rather than a Gang of Four pattern, listed here because it addresses the same problem.

The data

Patterns

NameIntentWhen to use
SingletonEnsure a class has only one instanceShared resource access, global state
Factory MethodCreate objects without specifying exact classDecoupling object creation from usage
Abstract FactoryCreate families of related objectsMultiple product variants
BuilderConstruct complex objects step by stepMany configuration options
PrototypeCreate new objects by copying existing onesObject creation is costly
Object PoolReuse a set of expensive-to-create objects instead of creating and destroying them on demandDatabase connections, threads and other costly resources
Lazy InitializationDelay creating an object or computing a value until it is first neededExpensive objects that may never be used
Dependency InjectionSupply an object's dependencies from outside instead of letting it construct themTesting, configuration and swapping implementations without changing callers

Fields

The shape of this dataset, read from the data itself: every column, its type and a real value. Agents can use this to plan a query without fetching the whole document.

SectionFieldTypeExample
patternsnamestringSingleton
patternsintentstringEnsure a class has only one instance
patternswhen_to_usestringShared resource access, global state

Fetch the same bytes

The static files contain the same reference rows as the API, with no API rate limit. Live metadata, such as view counts, may differ. Use the API for content negotiation; use the static files for a cacheable snapshot.

curl "https://yjtoon.yunusemrevurgun.com/api/dataset/creational-patterns?format=toon"
const res = await fetch(
  "https://yjtoon.yunusemrevurgun.com/static-data/dataset/creational-patterns.toon"
);
const toon = await res.text();

Rate limit: 120 requests per minute per IP, no key and no signup. API reference →

Topics

  • design patterns
  • oop
  • software engineering

Search the library

Search by a topic, title, or keyword.

Start with a collection

Browse all 205 datasets