Design Pattern

7 posts

12/19/2024

Strategy: Pulling Algorithms Out So Behavior Stays Swappable

Starting from a sprawling if-else discount calculator, see how the Strategy Pattern packages each algorithm into an interchangeable strategy object — and where the line falls between it and the State Pattern.

12/11/2024

Mediator: Untangling Communication Between Objects

Starting from a situation where objects call each other directly and the relationships turn into a tangled mess, see how the Mediator Pattern centralizes communication logic through a single mediator, and how it differs from the Observer Pattern.

12/7/2024

Observer: Subscriptions and State-Change Notifications

Starting from a scenario where one object's state change needs to notify multiple interested parties, see how the Observer Pattern decouples publishers from subscribers — and how it relates to the browser's native event system and Pub/Sub.

12/5/2024

Decorator: Extending an Object's Behavior at Runtime Without Touching Its Class

Starting from the trap of subclass explosion, this piece breaks down how the Decorator Pattern stacks behavior onto an object layer by layer, and clears up how it differs from JavaScript's own @decorator syntax.

11/20/2024

Facade: Hiding Complex Subsystems Behind One Unified Interface

Starting from a checkout flow that has to wire together several subsystems, this piece breaks down how the Facade Pattern wraps messy internals behind one simplified interface, and how it differs from the Adapter Pattern.

11/16/2024

Factory: Encapsulating and Abstracting Object Creation

Starting from `new` calls scattered across a codebase, see how the Factory Pattern centralizes object creation logic — and learn to tell Simple Factory, Factory Method, and Abstract Factory apart.

11/13/2024

Singleton: Guaranteeing One Global Instance, and What It Costs You

From hand-rolling a Singleton, to the traps it sets in multi-threaded and async environments, to why it gets called an anti-pattern — here's what this pattern actually buys you, and what it takes away.