Course contentsShow
← JavaScript
Lesson 1850 of 2,72338. Object-Oriented PatternsFree lesson

When to Use Each Pattern

Understand trade-offs and context: prefer simplicity, and apply patterns only when they solve real problems.

When to Use Each Pattern

You'll learn how to choose the right design pattern by understanding trade-offs and applying patterns only when they solve real problems.

The Golden Rule: Simplicity First

Design patterns are tools, not goals. Each pattern solves specific problems but adds complexity. Before reaching for any pattern, ask: "Does this actually simplify my code, or am I just applying patterns for their own sake?"

Pattern Selection Guide

Singleton: Use when you genuinely need exactly one instance (like a global config manager or logging service). Avoid if you're just trying to share data—modules already give you singletons naturally.

Factory/Builder: Choose factories for simple object creation with varying types. Use builders when constructing complex objects with many optional parameters step-by-step.

Module/Revealing Module: Pick these for encapsulation in older codebases or when you need private state without classes. Modern ES modules often handle this naturally.

Observer/Pub-Sub: Apply when you have one-to-many relationships where changes in one object should notify multiple dependents (UI updates, event systems).

Strategy: Use when you have multiple algorithms for the same task and need to swap them at runtime (payment processors, validation rules).

Command/Decorator/Proxy: These are for specialized cases—undo/redo systems (Command), adding behavior without modifying classes (Decorator), or controlling access/adding logging (Proxy).

Adapter: Only when integrating incompatible interfaces from third-party code.

The Real-World Test

If you find yourself writing complex pattern implementations just to handle simple logic, you've gone too far. Start with the simplest solution (plain functions, plain objects). Add patterns only when you experience actual pain points: difficult testing, rigid coupling, or repetitive code.

Key Takeaway: Prefer the simplest solution that works. Apply design patterns only when they demonstrably solve real problems in your codebase—not because they're clever or "proper."