Module 2 / Creational patterns
Module 2 · Core Patterns

Creational patterns

Control how objects are created — decouple clients from concrete classes and manage instance lifecycles.

0/3 lessons

Singleton

Learn

The Singleton ensures a class has only one instance and provides a single point of access to it. Common uses: configuration managers, logging services, connection pools, caches.

The classic implementation hides the constructor (private), stores the sole instance in a static field, and exposes it through a static method like `getInstance()`. Modern languages often prefer module-level exports or dependency injection containers over hand-rolled singletons.

class Logger { private static instance: Logger; private constructor() {} static getInstance(): Logger { if (!Logger.instance) Logger.instance = new Logger(); return Logger.instance; } }
Singletons make testing hard (global mutable state), hide dependencies, and create tight coupling. Prefer dependency injection unless you have a compelling reason for true singularity.
Practice
Loading…
Recall0/1
Recall

What is the main criticism of the Singleton pattern in modern software design?

Factory Method

Learn

The Factory Method defines an interface for creating an object but lets subclasses decide which class to instantiate. The creator class doesn't know the concrete product type — it delegates that decision.

This decouples the client from concrete classes. Adding a new product type means adding a new subclass, not modifying existing code (Open/Closed Principle).

// Creator defines the factory method abstract class DocumentCreator { abstract createDocument(): Document; // ← factory method open() { const doc = this.createDocument(); doc.render(); } } // Subclasses choose the concrete product class PdfCreator extends DocumentCreator { createDocument(): Document { return new PdfDocument(); } }
The Factory Method is about delegating instantiation to subclasses — not about choosing between products at runtime based on parameters (that's more like a Simple Factory or Abstract Factory).
Practice
Loading…
Recall0/1
Recall

In the Factory Method pattern, who decides which concrete product to instantiate?

Abstract Factory

Learn

The Abstract Factory provides an interface for creating families of related objects without specifying their concrete classes. Think of it as a factory of factories — each concrete factory produces a consistent set of products.

Classic example: a UI toolkit that must produce buttons, text fields, and checkboxes for either Windows or macOS. The client codes against the abstract factory interface; swapping themes means swapping the factory instance.

AspectFactory MethodAbstract Factory
CreatesOne productFamily of related products
MechanismSubclass overrides methodClient receives factory object
Variation axisPer-subclassPer-concrete-factory
ComplexityLowerHigher (more interfaces)
Abstract Factory adds significant interface overhead. Only use it when you genuinely need interchangeable product families — not for every case where multiple types exist.
Practice
Loading…
Recall0/1
Recall

What distinguishes Abstract Factory from Factory Method?