Skip to main content

Pattern Comparison

This page provides a comprehensive overview of all 23 design patterns organized by category. Use this as a quick reference to find the right pattern for your needs.

Creational Design Patterns

Creational patterns are focused towards how to instantiate an object or group of related objects. They deal with object creation mechanisms, trying to create objects in a manner suitable to the situation.
PatternPurposeWhen to Use
Simple FactoryGenerates an instance for client without exposing any instantiation logicWhen creating an object involves some logic beyond simple assignments and you want to avoid repeating the same code everywhere
Factory MethodDelegates the instantiation logic to child classesWhen there is generic processing in a class but the required sub-class is dynamically decided at runtime
Abstract FactoryA factory of factories; groups individual but related/dependent factories togetherWhen there are interrelated dependencies with not-that-simple creation logic involved
BuilderAllows creating different flavors of an object while avoiding constructor pollutionWhen there could be several flavors of an object and you need to avoid telescoping constructors. Key difference from factory: factory is one-step, builder is multi-step
PrototypeCreate object based on an existing object through cloningWhen an object is required that is similar to an existing object or when creation would be expensive compared to cloning
SingletonEnsures that only one object of a particular class is ever createdWhen exactly one object is needed to coordinate actions across the system (Note: considered an anti-pattern, use with caution)

Structural Design Patterns

Structural patterns are mostly concerned with object composition - how entities can use each other. They help answer “How to build a software component?”
PatternPurposeWhen to Use
AdapterWrap an otherwise incompatible object in an adapter to make it compatible with another classTo make existing classes work with others without modifying their source code
BridgePrefer composition over inheritance; push implementation details from a hierarchy to another object with a separate hierarchyWhen you want to decouple an abstraction from its implementation so that the two can vary independently
CompositeLets clients treat individual objects in a uniform mannerWhen you need to compose objects into tree structures to represent part-whole hierarchies
DecoratorDynamically change the behavior of an object at run time by wrapping them in decorator classesWhen you need to add behavior to individual objects dynamically without affecting other objects from the same class
FacadeProvides a simplified interface to a complex subsystemWhen you need to provide a simple interface to a complex system
FlyweightMinimize memory usage or computational expenses by sharing as much as possible with similar objectsWhen you need to use objects in large numbers and a simple repeated representation would use unacceptable memory
ProxyA class represents the functionality of another classWhen you need to add additional functionality (like caching, access control, etc.) before/after accessing the real object

Behavioral Design Patterns

Behavioral patterns are concerned with assignment of responsibilities between objects. They don’t just specify structure but also outline patterns for message passing/communication. They help answer “How to run a behavior in software component?”
PatternPurposeWhen to Use
Chain of ResponsibilityBuild a chain of objects; request enters from one end and keeps going from object to object till it finds the suitable handlerWhen you have multiple handlers for a request and you want to chain them in a specific order
CommandEncapsulate actions in objects; provides means to decouple client from receiverWhen you need to provide undo functionality or queue operations
IteratorProvides a way to access elements of an object without exposing the underlying presentationWhen you need to traverse a container and access its elements
MediatorAdds a third party object (mediator) to control interaction between two objects (colleagues)When you need to reduce coupling between classes communicating with each other
MementoCapture and store the current state of an object for later restorationWhen you need to provide undo functionality
ObserverDefines a dependency between objects so that whenever an object changes state, all dependents are notifiedWhen multiple objects need to be notified about state changes in another object
VisitorAdd further operations to objects without having to modify themWhen you need to add new operations to existing object structures without modifying those structures
StrategySwitch the algorithm or strategy based upon the situationWhen you want to select algorithm behavior at runtime
StateChange the behavior of a class when the state changesWhen you have an object that behaves differently based on its current state
Template MethodDefines the skeleton of how an algorithm could be performed, deferring implementation to children classesWhen you want to define the program skeleton of an algorithm but defer some steps to subclasses

Quick Pattern Selector

Need to Create Objects?

  • Simple logic: Simple Factory
  • Delegate to subclasses: Factory Method
  • Related object families: Abstract Factory
  • Complex construction: Builder
  • Copy existing object: Prototype
  • Single instance only: Singleton

Need to Compose Objects?

  • Make incompatible interfaces work: Adapter
  • Separate abstraction from implementation: Bridge
  • Tree structures: Composite
  • Add behavior dynamically: Decorator
  • Simplify complex system: Facade
  • Share data efficiently: Flyweight
  • Control access: Proxy

Need to Coordinate Behavior?

  • Chain of handlers: Chain of Responsibility
  • Encapsulate requests: Command
  • Traverse collections: Iterator
  • Reduce coupling between objects: Mediator
  • Capture/restore state: Memento
  • Notify multiple objects: Observer
  • Add operations without modification: Visitor
  • Switch algorithms: Strategy
  • State-dependent behavior: State
  • Define algorithm skeleton: Template Method

Pattern Relationships

Many patterns work well together:
  • Abstract Factory can be implemented using Factory Method or Prototype
  • Builder focuses on constructing complex objects step by step, while Abstract Factory emphasizes families of product objects
  • Composite and Decorator have similar structures but different intents
  • State can be interpreted as a Strategy that is able to switch the current strategy through method invocations

Build docs developers (and LLMs) love