Listing Vault SE322 · chapter 06 · Creational Design Patterns

Chapter 06 — Creational Design Patterns

GoF classification, the five creational patterns, and the question each one answers about what varies when an object is built.

06chapter
—lists
—total items
06

Creational Design Patterns

10 lists

GoF Classification 3 × 2

By purpose

  • Creational — how objects get created
  • Structural — how classes and objects are composed
  • Behavioral — how behavior and communication are distributed

By scope

  • Class — varies through inheritance
  • Object — varies through composition

A Pattern Entry Documents 6

  1. Intent
  2. Applicability
  3. Structure
  4. Participants
  5. Consequences
  6. Examples

The Five Creational Patterns 5

Pattern Scope Intent
Factory Method Class Define a creation operation and let subclasses choose the concrete product
Abstract Factory Object Create families of related products without naming concrete classes
Builder Object Build a complex object step by step, separating construction from representation
Prototype Object Create new objects by copying a configured prototype
Singleton Object Ensure one instance with a well-known access point

Factory Method — Key Points 4

  1. The creator uses the product but delegates the new to an overridable factory method
  2. Removes direct construction from client policy
  3. Use when the product type varies through extension
  4. Cost: extra classes and one more level of indirection

Abstract Factory — Key Points 4

  1. Enforces compatibility within a product family
  2. Clients depend on abstract product and factory interfaces
  3. Adding a new family is easy — one new class
  4. Adding a new product kind changes the interface and every factory

Builder — Key Points 4

  1. Handles optional parameters, validation and multiple representations
  2. A director can encode a reusable construction sequence
  3. Avoids telescoping constructors, at the cost of ceremony
  4. Validation belongs in build() — the one point where the object is complete

Prototype — Key Points 4

  1. Use when construction is expensive or runtime types vary
  2. Deep versus shallow copying must be a deliberate decision
  3. Hazards: identity, shared references, mutable state
  4. A prototype registry lets clients ask for a copy by key

Singleton — Key Points 4

  1. Complications: concurrency, lifecycle, serialization, class loaders
  2. A naive lazy getInstance() is not thread-safe
  3. Fixes: eager static field, double-checked locking, or an enum
  4. Costs: global state, hidden dependencies, harder testing

Pattern Selection Guide 4

What varies Pattern
Many kinds of one product, chosen by extension Factory Method
Families of products that must stay consistent Abstract Factory
One product with many optional parts or representations Builder
Expensive or run-time-configured setup to reuse Prototype
Singularity is a genuine invariant Singleton

Selection Ground Rules 3

  1. Start with a plain constructor; add a pattern when the construction decision itself changes
  2. Prefer the simplest mechanism that preserves the required flexibility
  3. Patterns combine — an Abstract Factory is often a Singleton whose methods are Factory Methods