Skip to main content

Summary

Pattern Comparison

PatternWhen to ApplyStrengthsCaveatsCommon Use Cases
StateWhen behavior varies based on internal stateClear separation of responsibilities and transitionsCan be overkill with few statesStep flows, workflows, UI mode switching
StrategyWhen behavior changes based on external inputEasy to swap logicState transition logic handled elsewhereAlgorithm switching, discount strategies
CommandWhen focusing on operations, not statesEasy to manage execution history, Undo/RedoNot ideal for state transitionsBatch processing, queues, operation logs

Choosing the Right Pattern

  • If conditionals increase based on internal state → consider State
  • If you only need to swap behavior without transitions → use Strategy
  • If the focus is on actions and their history → Command is a better fit

Team Discussion Snippets

  • “This behavior changes with state—let’s extract it using the State pattern to improve testability.”
  • “We’re just switching logic based on inputs—Strategy might be a cleaner choice.”
  • “Since we’re more focused on command execution and history, Command could be a better abstraction.”