Skip to main content

Summary

Pattern Comparison

PatternWhen to ApplyStrengthsCaveatsCommon Use Cases
FacadeWhen you want to encapsulate complex logicProvides a simple interfaceCan blur responsibilities if overusedAPIs, domain services, system façade
StrategyWhen switching algorithms or behaviorsExcellent extensibility and testabilityMay lead to many small classesDiscount strategies, sorting logic
StateWhen behavior depends on object stateExplicitly separates state and behaviorOverengineering if states are fewWorkflow, UI modes, state machines
CompositeWhen working with hierarchical structuresSimplifies recursion and provides flexibilityHard to manage in very deep structuresMenus, org charts, trees
IteratorWhen custom collection traversal is neededStandardizes and abstracts traversalOverkill for simple arraysCustom collections, tree traversal

Choosing the Right Pattern

  • Suspect a God Object when a class grows too large and handles too much
  • The key is to clearly separate responsibilities into smaller classes:
    • Separate processing logic using Facade or Strategy
    • Offload state control with the State pattern
    • Structure hierarchical data using Composite
    • Abstract collection traversal using Iterator
  • It’s not just about having many methods—focus on which responsibilities are bloated

Team Discussion Snippets

  • "Isn't this class doing too much? Let's break it down with a Facade."
  • "Since behavior depends on state, we could extract it using the State pattern."
  • "This logic varies by case—Strategy would make it more testable."
  • "This looks like a nested structure—Composite could make the recursion cleaner."
  • "Too many raw loops over this array—how about introducing an Iterator?"