Behavioral PatternsStrategy PatternHard⏱️ ~3 min

Strategy Pattern - When to Use and Trade-offs

When to Use Strategy Pattern

Strategy Pattern is appropriate when these conditions exist:

First, multiple algorithm variants exist: You have several ways to accomplish the same task (sorting algorithms, compression methods, payment gateways) and need to switch between them. If you only have one algorithm now and no realistic expectation of variants, Strategy is premature abstraction.

Second, algorithm selection happens at runtime: The choice depends on user input, configuration, or runtime conditions. If the algorithm is known at compile time and never changes, a simple method call suffices without pattern overhead.

Third, algorithms are complex enough to warrant separate classes: Each variant has substantial logic (more than 20-30 lines). For trivial variations (changing a single constant), a parameter or configuration value is simpler than full Strategy classes.

Fourth, you want to avoid conditional bloat: Without Strategy, your code would be filled with if-else or switch statements. If you only have 2-3 simple conditions, a conditional might be clearer than introducing multiple classes.

When Strategy Pattern is Overkill

Scenario: Simple Toggle

Problem: Calculating price with or without tax.

Overkill Solution: Create TaxIncludedPricing and TaxExcludedPricing strategies.

Better Solution: Pass a boolean parameter includeTax to a single calculation method. The logic difference is one line (multiply by 1.0 or 1.1), not worth two classes.
Scenario: Static Configuration

Problem: Different database connection pools for dev and production.

Overkill Solution: Create DevConnectionStrategy and ProdConnectionStrategy.

Better Solution: Use a configuration file with connection parameters. The algorithm (creating a connection pool) is identical, only parameters differ. Strategy Pattern solves behavioral differences, not data differences.

Strategy vs. Alternatives

Strategy Pattern
Use when: Multiple interchangeable algorithms exist and selection happens at runtime.

Behavior: Context delegates to strategy. Algorithms are independent and swappable.

Example: Payment processing with multiple gateways.
State Pattern
Use when: Object behavior changes based on internal state transitions.

Behavior: States trigger state changes. Not freely swappable by client.

Example: Vending machine states (idle, accepting money, dispensing).
Strategy Pattern
Use when: Encapsulating algorithms within classes.

Structure: Multiple strategy classes with single interface method.

Example: Sorting strategies (quicksort, mergesort).
Template Method
Use when: Algorithm structure is fixed, but steps vary.

Structure: Abstract class with template method calling overridable hooks.

Example: Data parsers with fixed flow (open, parse, close) but varying parse logic.

Trade-offs

Advantages: First, eliminates conditional logic for algorithm selection. Second, enables Open/Closed Principle compliance (add strategies without modifying context). Third, each strategy is independently testable. Fourth, strategies can be reused across different contexts.

Disadvantages: First, increases number of classes (each algorithm becomes a class). Second, clients must be aware of different strategies to select appropriately, unless a Factory hides this. Third, all strategies must share the same interface, which may force awkward parameter passing if algorithms need different data. Fourth, if strategies need shared state, managing it becomes complex (pass via constructor, method parameters, or context reference).

Interview Tip: When asked about drawbacks, mention the "strategy explosion" problem. If you have 10 vehicle types and 5 pricing models (weekend, holiday, surge, early bird, loyalty), do you create 50 classes? Answer: No. Use composition (Decorator Pattern) or a chain of strategies to combine behaviors instead.
💡 Key Takeaways
Use Strategy when multiple complex algorithm variants exist and runtime selection is needed
Avoid Strategy for simple toggles, static configuration, or trivial logic differences
Strategy eliminates conditionals but increases class count
Differs from State (internal transitions) and Template Method (fixed algorithm structure)
Consider alternatives like configuration parameters or Decorator for composing behaviors
📌 Examples
1Appropriate: Payment gateways with different APIs and error handling
2Overkill: Including/excluding tax in price calculation
3Alternative: State Pattern for order status (pending, shipped, delivered)
4Alternative: Template Method for report generation with fixed steps
← Back to Strategy Pattern Overview