Skip to content

Design Patterns

Design patterns are proven, reusable solutions to common problems that arise in software design. First cataloged by the “Gang of Four” (Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides) in their landmark 1994 book Design Patterns: Elements of Reusable Object-Oriented Software, these patterns remain foundational knowledge for every software engineer.

Why Design Patterns Matter

Design patterns provide a shared vocabulary for developers. Instead of explaining a complex design decision from scratch, you can say “we used a Strategy pattern here” and your team immediately understands the architecture. They also:

  • Accelerate development by providing tested solutions to recurring problems
  • Improve code quality through well-structured, maintainable designs
  • Facilitate communication with a common language among developers
  • Reduce risk by leveraging solutions that have been refined over decades
  • Support SOLID principles and clean architecture naturally

The Three Categories

Design patterns are organized into three categories based on their purpose:

When to Use Design Patterns

Use Patterns When

  • You recognize a recurring problem that a pattern solves cleanly
  • You need to decouple components for flexibility and testability
  • Your code is becoming rigid or fragile under changing requirements
  • You want to communicate intent clearly to other developers
  • You need to follow the Open/Closed Principle (open for extension, closed for modification)

Avoid Patterns When

  • The problem is simple enough that a pattern adds unnecessary complexity
  • You are forcing a pattern where a direct solution is clearer
  • The pattern introduces over-engineering for a prototype or throwaway code
  • You do not fully understand the pattern and its trade-offs
  • A language feature (closures, first-class functions, generics) already solves the problem natively

Quick Reference Table

Creational Patterns

PatternIntentCommon Use Cases
SingletonEnsure a class has only one instanceConfiguration managers, logging, connection pools
Factory MethodDefine an interface for creating objects, letting subclasses decide which class to instantiateUI component libraries, plugin systems, document parsers
Abstract FactoryCreate families of related objects without specifying concrete classesCross-platform UI toolkits, database access layers
BuilderConstruct complex objects step by stepQuery builders, configuration objects, HTTP requests
PrototypeCreate new objects by copying existing onesGame entities, document templates, cached objects

Structural Patterns

PatternIntentCommon Use Cases
AdapterConvert an interface into another that clients expectLegacy system integration, third-party library wrappers
BridgeSeparate abstraction from implementation so both can varyCross-platform rendering, device drivers, persistence layers
CompositeCompose objects into tree structures for part-whole hierarchiesFile systems, UI component trees, organizational charts
DecoratorAttach additional responsibilities to objects dynamicallyI/O streams, middleware pipelines, logging wrappers
FacadeProvide a simplified interface to a complex subsystemAPI gateways, library wrappers, service orchestration
ProxyProvide a surrogate or placeholder to control accessLazy loading, access control, caching, remote objects

Behavioral Patterns

PatternIntentCommon Use Cases
ObserverDefine a one-to-many dependency so dependents are notified of changesEvent systems, reactive UI, pub/sub messaging
StrategyDefine a family of interchangeable algorithmsSorting algorithms, payment processing, validation rules
CommandEncapsulate a request as an objectUndo/redo systems, task queues, macro recording
StateAllow an object to alter its behavior when its internal state changesWorkflow engines, TCP connections, game character AI
IteratorProvide a way to access elements of a collection sequentiallyCollection traversal, database cursors, stream processing
Template MethodDefine the skeleton of an algorithm, deferring steps to subclassesFrameworks, data pipelines, test fixtures

Relationships Between Patterns

Patterns often complement each other. Understanding these relationships helps you choose the right combination:

Creational Relationships:
Abstract Factory ──uses──► Factory Method
Builder ──can build──► Composite
Prototype ──alternative to──► Factory Method
Structural Relationships:
Adapter ──similar to──► Bridge (but different intent)
Composite ──often uses──► Iterator
Decorator ──similar to──► Proxy (but different intent)
Facade ──simplifies──► Complex subsystems
Behavioral Relationships:
Strategy ──similar to──► State (but different intent)
Observer ──often uses──► Mediator
Command ──can use──► Memento (for undo)
Iterator ──traverses──► Composite
Template Method ──alternative to──► Strategy (inheritance vs composition)

How to Study Design Patterns

  1. Understand the problem first — Know what problem each pattern solves before memorizing the structure
  2. Study the UML/structure — Understand the participants and their relationships
  3. Read real-world examples — See how patterns are used in frameworks you already know
  4. Implement from scratch — Code each pattern yourself in your preferred language
  5. Recognize patterns in existing code — Look for patterns in open-source libraries
  6. Practice refactoring — Take messy code and refactor it using appropriate patterns

Phase 1: Essential Patterns

Duration: 1-2 weeks

Start with the most commonly used patterns that you will encounter in everyday development.

Start with: Singleton, Factory Method, Observer, Strategy, Decorator

Prerequisites: Basic OOP knowledge (classes, interfaces, inheritance)

Phase 2: Structural Mastery

Duration: 1-2 weeks

Learn how to compose objects into flexible, maintainable structures.

Focus on: Adapter, Facade, Composite, Proxy, Builder

Prerequisites: Phase 1 patterns

Phase 3: Advanced Patterns

Duration: 2-3 weeks

Master the remaining behavioral patterns and learn to combine patterns effectively.

Focus on: Command, State, Template Method, Iterator, Abstract Factory, Bridge, Prototype

Prerequisites: Phases 1 and 2

Begin Learning