Choose the right behavioral design pattern from the 11 GoF behavioral patterns. Use when someone asks "how do I make this algorithm swappable?", "should I us...
- You have algorithms that vary across contexts and clients should swap them independently
- An object must change its behavior radically when its internal state changes
- You need to issue requests without knowing the receiver or the operation in advance
- Multiple objects are communicating in complex, entangled ways and need decoupling
- You need to traverse or operate on a collection without exposing its internals
- You want to add operations to a stable class hierarchy without modifying it
- You need to save and restore an object's internal state without breaking encapsulation
- You want to define a grammar for simple sentences and interpret them
- You are deciding among behavioral patterns and want an explicit trade-off comparison
Before starting, confirm:
- Is the problem genuinely behavioral (communication/algorithm), not creational (object construction) or structural (interface composition)? If unsure, invoke `design-pattern-selector` first for purpose classification.
- Do you have at least a rough description of what the system needs to do differently β even informally?
---
π‘ Examples
Example 1: Order Processing with Multiple Tax Algorithms
Scenario: An e-commerce system computes tax differently depending on region (US federal, EU VAT, Canadian GST). The tax logic is embedded in the Order class as a large if/else block. A new region is coming and the team does not want to keep modifying Order.
Trigger: "We have different tax rules per region and the order class keeps getting bigger every time we add one."
Process:
Step 2 (Taxonomy 1): What varies is the *algorithm* for tax computation. Clients (or configuration) choose which algorithm to apply. β Strategy
Step 3 (Taxonomy 2): No sender-receiver decoupling concern β Order calculates tax directly.
Step 4: Strategy vs. State? The variation is client-selected (region at order creation), not self-transitioning behavior. Confirmed: Strategy.